Javascript學習筆錄10(字符串的操作)

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 gfdg
document.write(str1.substr (20,11)+"<br>")  //dsf cccc dd   f cccc dddd
alert("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#end
str1="this is the end of the line.<br>"
document.write(str1)
document.write("字符end在字符串的位置是"+str1.search("end")+"</br>")//index=0
document.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>



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章