Javascript学习笔录10(字符串的操作)

来源:互联网 发布:ubuntu的使用教程 编辑:程序博客网 时间:2024/06/05 07:49

1 字符串字体加粗,字体大写

string.bold()

string.toUpperCase()

2截取字段

  String.substr(N1,N2) 这个就是我们常用的从指定的位置(N1)截取指定长度(N2)的字符串; 
   String.substring(N1,N2) 这个就是我们常用的从指定的位置(N1)到指定的位置之前(N2)的字符串;  

  个数从0开始 
  举个例子:
  document.write(str1.substring(0,13)+"<br>") //fdsf1111 gfdg
  document.write(str1.substr (20,11)+"<br>")  // f cccc dddd

3 链接字符串

  str1.concat

  str1+=str2

4 格式化字符串

   字符串.格式化方式()

   document.write(str1.italics()) //斜体

5 创建锚

    str1="begin ,update this node";
    str2="link to anchor";
    document.write(str1.anchor("anc"));

    document.write(str2.link("#anc"))

6 搜索字符串

    str1.search("end")

7 定位字符串中的字符

   strObj.indexOf(subString[, startIndex])

返回 String 对象内第一次出现子字符串的字符位置

document.write("the index for the second word ‘and' is:"+str1.indexOf("and",30)+"</br>")//从第30个字符之后,出现and字符位置

  strObj.lastIndexOf(substring[, startindex])
 返回 String 对象中子字符串最后出现的位置。
 参数: startindex  可选项。

 该整数值指出在 String 对象内进行查找的开始索引位置。如果省略,则查找从字符串的末尾开始。
  如果没有找到子字符串,则返回 -1。
  如果 startindex 是负数,则 startindex 被当作零。如果它比最大字符位置索引还大,则它被当作最大的可能索引。
 从右向左执行查找。否则,该方法和 indexOf 相同。

8 字符串替换

 stringObject.replace(regexp/substr,replacement)

参数: regexp 具有全局标志 g,那么 replace() 方法将替换所有匹配的子串。否则,它只替换第一个匹配子串。

document .write(str1.replace("and",","))//替换第一个and =>,

具体代码:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Js8.aspx.cs" Inherits="Javascript_Js8" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>Untitled Page</title></head><body>    <form id="form1" runat="server">    <div>        </div>    </form></body></html><script>myString="dsjfsdfjsdfsdfhsfiuhfihehe";document.write(myString+"</br>");document.write(myString.bold()+"</br>");document.write(myString.toUpperCase()+"</br>");str1="fdsf1111 gfdgfd dfdsf cccc dddd.<br>"document.write(str1)document.write(str1.substring(0,13)+"<br>") //fdsf 1111 gf  fdsf1111 gfdgdocument.write(str1.substr (20,11)+"<br>")  //dsf cccc dd   f cccc ddddalert("123456789".substr(2,5)) alert("123456789".substring(2,5)) str1="I Like";str2="basketball and football and so on ";document.write(str1+"</br>")document.write(str2+"</br>")document.write(str1.concat(str2)+"</br>")document.write(str1+=str2)str1="food and meat</br>"document.write(str1)document.write(str1.italics())document.write(str1.strike())str1="this is the bigginning of the page.<br>"str2="….<br>"str3="this is the end of the page .<br>"str4="link to the start<br>"str5="link to the end<br>"str6="Link to the anchor1"document.write(str1.anchor("start"))//输出str1,同时,给str1创建一个名叫start的锚,锚的值this is the bigginning of the page.for(i=0;i<100;i++)document.write(str2);document.write(str3.anchor("end"))document.write(str4.link("#start"))document.write(str5.link("#end"))//str5 格式化为link,并绑定锚 http://localhost:25655/jQuery/Javascript/Js8.aspx#endstr1="this is the end of the line.<br>"document.write(str1)document.write("字符end在字符串的位置是"+str1.search("end")+"</br>")//index=0document.write("字符dog在字符串的位置是"+str1.search("dog")+"</br>")str1="spring is a time for flowers and trees and much hope and happy<br>"document.write(str1+"</br>")document.write("the index for the second word ‘and' is:"+str1.indexOf("and",30)+"</br>")document.write("the last index of the word ‘and' is "+str1.lastIndexOf("and")+"</br>")str1="spring is a time for flowers and trees and baby bunnles<br>"document.write(str1)document .write(str1.replace("and",","))str1="spring is a time for flowers and trees and much hope and happy<br>"document.write(str1)str1array=str1.split(" ")document.write(str1array[0]+"<br>")document.write(str1array[1]+"<br>")document.write(str1array[2]+"<br>")document.write(str1array[3]+"<br>")</script>