字符串常用方法
阿新 • • 發佈:2017-10-30
字符串 方法
字符串常用方法
Method | 描述 |
charAt() | 返回指定索引位置的字符 |
charCodeAt() | 返回指定索引位置字符的 Unicode 值 |
concat() | 連接兩個或多個字符串,返回連接後的字符串 |
fromCharCode() | 將字符轉換為 Unicode 值 |
indexOf() | 返回字符串中檢索指定字符第一次出現的位置 |
lastIndexOf() | 返回字符串中檢索指定字符最後一次出現的位置 |
localeCompare() | 用本地特定的順序來比較兩個字符串 |
match() | 找到一個或多個正則表達式的匹配 |
replace() | 替換與正則表達式匹配的子串 |
search() | 檢索與正則表達式相匹配的值 |
slice() | 提取字符串的片斷,並在新的字符串中返回被提取的部分 |
split() | 把字符串分割為子字符串數組 |
substr() | 從起始索引號提取字符串中指定數目的字符 |
substring() | 提取字符串中兩個指定的索引號之間的字符 |
toLocaleLowerCase() | 根據主機的語言環境把字符串轉換為小寫,只有幾種語言(如土耳其語)具有地方特有的大小寫映射 |
toLocaleUpperCase() | 根據主機的語言環境把字符串轉換為大寫,只有幾種語言(如土耳其語)具有地方特有的大小寫映射 |
toLowerCase() | 把字符串轉換為小寫 |
toString() | 返回字符串對象值 |
toUpperCase() | 把字符串轉換為大寫 |
trim() | 移除字符串首尾空白 |
valueOf() | 返回某個字符串對象的原始值 |
例子:
<script> var a = "1234"; var b = "abcde"; // charAt() 返回指定索引位置的字符 //console.log(a.charAt(0));//1 //charCodeAt() 返回指定索引位置字符的 Unicode 值 //console.log(b.charCodeAt(0));//97 //concat() 連接兩個或多個字符串,返回連接後的字符串 //console.log(a.concat(b)); //1234abcde //fromCharCode() 將字符轉換為 Unicode 值 //document.write(String.fromCharCode(72,69,76,76,79));//HELLO //indexOf() 返回字符串中檢索指定字符第一次出現的位置,如果沒有,則返回-1 //console.log(b.indexOf("x"));//1 //lastIndexOf() 返回字符串中檢索指定字符最後一次出現的位置 //var c = "aba"; //console.log(c.lastIndexOf("a"));//2 //localeCompare() 用本地特定的順序來比較兩個字符串 //match() 找到一個或多個正則表達式的匹配 //var str="1 plus 2 equal 3" //document.write(str.match(/\d+/g));//1,2,3 們將使用全局匹配的正則表達式來檢索字符串中的所有數字: //replace() 替換與正則表達式匹配的子串 //document.write(a.replace("1","a"));//a234 //search() 檢索與正則表達式相匹配的值 //var str1="Hello world!" //document.write(str1.search("world") + "<br />");//6 //slice() 提取字符串的片斷,並在新的字符串中返回被提取的部分 //document.write(b.slice(1,2));//b //split() 把字符串分割為子字符串數組 var f = "a,c,"; //document.write(f.split(","));//a,c //substr() 從起始索引號提取字符串中指定數目的字符 // document.write(f.substr(0,2));//a, //substring() 提取字符串中兩個指定的索引號之間的字符 //document.write(f.substring(0,1));//a //toLocaleLowerCase() 根據主機的語言環境把字符串轉換為小寫,只有幾種語言(如土耳其語)具有地方特有的大小寫映射 //toLocaleUpperCase() //根據主機的語言環境把字符串轉換為大寫,只有幾種語言(如土耳其語)具有地方特有的大小寫映射 //var d = "abE"; //document.write(d.toLocaleUpperCase());//ABE //toLowerCase() 把字符串轉換為小寫 //var e = "abE"; //document.write(e.toLowerCase());//abe //toString() 返回字符串對象值 //document.write(b.toString());//abcde //toUpperCase() 把字符串轉換為大寫 // document.write(b.toUpperCase());//ABCDE //trim() 移除字符串首尾空白 // document.write(a.trim());//1234 //valueOf() 返回某個字符串對象的原始值 document.write(a.valueOf());//1234 </script>
本文出自 “JianBo” 博客,請務必保留此出處http://jianboli.blog.51cto.com/12075002/1977304
字符串常用方法