string 物件屬性和方法
string 物件屬性
constructor:對建立該物件的函式的引用;
length:字串的長度;
prototype:允許像物件新增屬性和方法
string 物件方法
var str1="123456";
var str2="asdfgh";
1:
document.write(str1);
document.write(str2+"<br>");
//用大號字型顯示字串,字型變大
document.write(str1.big())
document.write(str2.big()+"<br>")
//顯示閃動字串,無法用於ie中
document.write(str1.blink())
document.write(str2.blink()+"<br>")
//使用粗體顯示字串,字型加粗
document.write(str1.bold())
document.write(str2.bold()+"<br>")
2://返回在指定位置的字元
document.write(str1.charAt(2))
document.write(str2.charAt(4))
//返回在指定位置的字元
document.write(str1.charCodeAt(2))
document.write(str2.charCodeAt(4))
//連線字串,跟陣列的方法類似
document.write(str1.concat(str2))
// 以打字機文字顯示字串
document.write(str1.fixed())
document.write(str2.fixed())
3: //使用指定的顏色來顯示字串
document.write(str1.fontcolor("pink")) //改變顏色,把str1設定粉紅色,str2設定為青色
document.write(str2.fontcolor("cyan"))
//使用指定的尺寸來顯示字串,設定了字型的大小
document.write(str1.fontsize(8))
document.write(str2.fontsize(8))
4://從字元編碼建立一個字串,不需要字串
document.write(String.fromCharCode(72,69,76,76)+"<br>")
document.write(String.fromCharCode(65,66)+"<br>")
//檢索字串,str1檢索的是3的位置,str2檢索的是h的位置
document.write(str1.indexOf(3)+"<br>")
document.write(str2.indexOf("h")+"<br>")
//使用斜體顯示字串
document.write(str1.italics()+"<br>")
document.write(str2.italics()+"<br>")
//從後向前搜尋字串,str1檢索的是4,str2檢索的是f
document.write(str1.lastIndexOf(4)+"<br>")
document.write(str2.lastIndexOf("f")+"<br>")
5: //將字串顯示為連結 ,此處將str1連結為百度,str2連結為w3school
document.write(str1.link("http://www.baidu.com.cn")+"<br>")
document.write(str2.link("http://www.w3school.com.cn")+"<br>")
6: //找到一個或多個正則表示式的匹配,str1匹配的是123,str2匹配的是asd
document.write(str1.match(123) + "<br />")
document.write(str2.match("asd")+ "<br />")
//替換與正則表示式匹配的子串,此處,用789替換的456,用jkl替換的fgh
document.write(str1.replace(/456/, 789)+ "<br />")
document.write(str2.replace(/fgh/, "jkl")+ "<br />")
//檢索與正則表示式相匹配的值,str1檢索的是6的位置,str2檢索的是g的位置
document.write(str1.search(/6/)+ "<br />")
document.write(str2.search(/g/)+ "<br />")
7: 在此處將str1中的數字分開了,所以3的索引變成了3,檢索出來的結果就是34 56
var str1="12 34 56";
var str2="asdfgh";
//提取字串的片斷,並在新的字串中返回被提取的部分
document.write(str1.slice(3)+"<br>")
document.write(str2.slice(4)+"<br>")
//使用小字號來顯示字串
document.write(str1.small()+"<br>")
document.write(str2.small()+"<br>")
8:在此處將str1中的數字分開了,所以3的索引變成了3,檢索出來的結果就是34 56
var str1="12 34 56";
var str2="asdfgh";
//把字串分割為字串陣列
1:document.write(str1.split(" ") + "<br />") //split裡面的括號中有空格
2:document.write(str1.split("") + "<br />") //split裡面的括號中沒有有空格
3:document.write(str1.split(" ",4)+ "<br />")
1:document.write(str2.split(" ") + "<br />")
2:document.write(str2.split("") + "<br />")
3:document.write(str2.split(" ",4))
9: //使用刪除線來顯示字串
document.write(str1.strike()+ "<br />")
document.write(str2.strike()+ "<br />")
//把字串顯示為下標
document.write(str1.sub()+ "<br />")
document.write(str2.sub()+ "<br />")
//把字串顯示為上標
document.write(str1.sup()+ "<br />")
document.write(str2.sup()+ "<br />")
10: //從起始索引號提取字串中指定數目的字元
document.write(str1.substr(2)+ "<br />")
document.write(str2.substr(4)+ "<br />")
//提取字串中兩個指定的索引號之間的字元
document.write(str1.substring(3)+ "<br />")
document.write(str2.substring(3)+ "<br />")
11://把字串顯示為上標
document.write(str1.toLocaleLowerCase()+ "<br />")
document.write(str2.toLocaleLowerCase()+ "<br />")
//把字串轉換為小寫
document.write(str1.toLocaleUpperCase()+ "<br />")
document.write(str2.toLocaleUpperCase()+ "<br />")
//把字串轉換為大寫
document.write(str1.toLowerCase()+ "<br />")
document.write(str2.toLowerCase()+ "<br />")
//把字串轉換為小寫
document.write(str1.toUpperCase()+ "<br />")
document.write(str2.toUpperCase()+ "<br />")
12://用本地特定的順序來比較兩個字串
返回值
說明比較結果的數字。如果 stringObject 小於 target,則 localeCompare() 返回小於 0 的數。如果 stringObject 大於 target,則該方法返回大於 0 的數。如果兩個字串相等,或根據本地排序規則沒有區別,該方法返回 0。
說明
把 < 和 > 運算子應用到字串時,它們只用字元的 Unicode 編碼比較字串,而不考慮當地的排序規則。以這種方法生成的順序不一定是正確的。例如,在西班牙語中,其中字元 “ch” 通常作為出現在字母 “c” 和 “d” 之間的字元來排序。
localeCompare() 方法提供的比較字串的方法,考慮了預設的本地排序規則。ECMAscript 標準並沒有規定如何進行本地特定的比較操作,它只規定該函式採用底層作業系統提供的排序規則
str.sort (function(a,b){return a.localeCompare(b)})
//to string 返回字串
stringObject.toString()// valueOf() 返回某個字串物件的原始值 ,當呼叫該方法的物件不是 String 時丟擲 TypeError 異常 stringObject.valueOf()