js中String型別的常用方法
阿新 • • 發佈:2018-11-20
js中String型別的常用方法
var str1 = new String(“hello”);
var str2 = new String(“hello”);
document.write(“兩個字串的物件一樣嗎?”+(str1.toString()==str2.toString()));
建立一個字串的方式:
方式1:
new String(“字串的內容”);
方式2:
var str = “字串的內容”;
字串常用的方法:
anchor() 生產錨點
blink() 為元素新增blink標籤
charAt() 返回指定索引位置處的字元。
charCodeAt() 回一個整數,代表指定位置上字元的 Unicode 編碼。
fontcolor() 把帶有 COLOR 屬性的一個 HTML 標記放置在 String 物件中的文字兩端
indexOf() 返回 String 物件內第一次出現子字串的字元位置
italics() 把 HTML 標記放置在 String 物件中的文字兩端。
link() 把一個有 HREF 屬性的 HTML 錨點放置在 String 物件中的文字兩端。
replace() 返回根據正則表示式進行文字替換後的字串的複製
split() 切割
Substr() 擷取子串
toUpperCase() 轉大寫
toLowerCase 轉小寫
<!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> <script type="text/javascript"> document.write("第五章".anchor("five")+"<br/>"); document.write("第五章".blink()+"<br/>"); document.write("abc".charAt(1)+"<br/>"); document.write("abc".charCodeAt(1)+"<br/>"); //chatCodeAt返回的是索引值對應的字元的碼值。 document.write("第六章".fontcolor("red")+"<br/>"); //fontcolor() 給字串新增font標籤,然後設定color的屬性值。 document.write("abchellohehehello".indexOf("hello")+"<br/>"); //返回指定字串第一次出現的索引值。 document.write("第五章".italics()+"<br/>"); //給文字新增一個i標籤,把文字內容設定成斜體。 document.write("百度".link("http://www.baidu.com")+"<br/>"); // 給文字新增一個a標籤, document.write("明天我們講xml".replace("xml","DOM程式設計")+"<br/>"); // 給文字新增一個a標籤, var str = "我們-大家-好"; var arr = str.split("-"); for(var index = 0 ; index<arr.length ; index++){ document.write(arr[index]+","); } document.write("<br/>"); document.write("abc".toUpperCase()+"<br/>"); //轉大寫 document.write("ABC".toLowerCase()+"<br/>"); //轉小寫 </script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文件</title> </head> <body> </body> </html>