String對象使您可以處理一系列字符。它使用許多輔助方法包裝JavaScript的字符串原始數據類型。當JavaScript在字符串基元和String對象之間自動轉換時,您可以在字符串基元上調用String對象的任何輔助方法。
創建String對象:var val = new String(string);
String 屬性
Sr.No | 屬性和說明 |
---|---|
1 | constructor
返回對創建對象的String函數的引用。 |
2 | length
返回字符串的長度。 |
3 | Prorotype
原型屬性使您可以向對象添加屬性和方法。 |
var str = new String( "This is string" );
console.log("str.constructor is:" + str.constructor)
//輸出:str.constructor is:function String() { [native code]
var uname = new String("Hello World")
console.log(uname) //輸出:Hello World
console.log("Length "+uname.length) //輸出:Length 11
String 方法
Sr.No | Method & Description |
---|---|
1 | charAt()
返回指定索引處的字符。 |
2 | charCodeAt()
返回一個數字,該數字指示給定索引處字符的Unicode值。 |
3 | concat()
合併兩個字符串的文本並返回一個新字符串。 |
4 | indexOf()
返回指定值的第一次出現在調用String對象內的索引;如果未找到,則返回-1。 |
5 | lastIndexOf()
返回最後一次出現的指定值的調用String對象內的索引;如果未找到,則返回-1。 |
6 | localeCompare()
返回一個數字,該數字指示參考字符串是按排序順序排在給定字符串之前還是之後還是與之相同。 |
7 | match()
用於將正則表達式與字符串匹配。 |
8 | replace()
用於查找正則表達式和字符串之間的匹配項,並將匹配的子字符串替換爲新的子字符串。 |
9 | search()
搜索正則表達式和指定字符串之間的匹配項。 |
10 | slice()
提取字符串的一部分並返回新的字符串。 |
11 | split()
通過將字符串對象拆分爲子字符串,將String對象拆分爲字符串數組。 |
12 | substr()
從指定位置開始通過指定數量的字符返回字符串中的字符。 |
13 | substring()
返回字符串中兩個索引之間的字符串中的字符。 |
14 | toLocaleLowerCase()
遵守當前語言環境時,字符串中的字符將轉換爲小寫。 |
15 | toLocaleupperCase()
遵守當前語言環境時,字符串中的字符將轉換爲大寫。 |
16 | toLowerCase()
返回轉換爲小寫的字符串值。 |
17 | toString()
返回表示指定對象的字符串。 |
18 | toUpperCaer()
返回轉換爲大寫的字符串值。 |
19 | valueOf()
返回指定對象的原始值。 |
charAt()是從指定索引返回字符的方法。字符串中的字符從左到右索引。第一個字符的索引爲0,字符串中最後一個字符的索引爲stringName,爲stringName.length – 1。
charCodeAt方法返回一個數字,該數字指示給定索引處字符的Unicode值。Unicode代碼點的範圍是0到1,114,111。前128個Unicode代碼點是ASCII字符編碼的直接匹配項。charCodeAt()始終返回小於65,536的值。
var str = new String("This is string"); console.log("str.charAt(0) is:" + str.charAt(0)); //輸出:str.charAt(0) is:T console.log("str.charAt(0) is:" + str.charCodeAt(0)); //輸出: str.charAt(0) is:84 console.log("str.charAt(1) is:" + str.charAt(1)); //輸出:str.charAt(1) is:h console.log("str.charAt(1) is:" + str.charCodeAt(1));//輸出:str.charAt(1) is:104 console.log("str.charAt(2) is:" + str.charAt(2)); //輸出:str.charAt(2) is:i console.log("str.charAt(3) is:" + str.charAt(3)); //輸出:str.charAt(3) is:s console.log("str.charAt(4) is:" + str.charAt(4)); //輸出:str.charAt(4) is: console.log("str.charAt(5) is:" + str.charAt(5)); //輸出:str.charAt(5) is:i
concat方法添加兩個或多個字符串,並返回一個新的單個字符串。
語法:string.concat(string2, string3[, ..., stringN]);
var str1 = new String( "This is string one" );
var str2 = new String( "This is string two" );
var str3 = str1.concat( str2 );
console.log("str1 + str2 : "+str3)
//輸出:str1 + str2 : This is string oneThis is string two
indexOf方法返回指定值首次出現的調用String對象內的索引,如果沒有找到該值,則從fromIndex或-1開始搜索。
語法:string.indexOf(searchValue[, fromIndex])
lastIndexOf方法返回最後一次出現的指定值的調用String對象中的索引,如果沒有找到該值,則從fromIndex或-1開始搜索。
語法:string.lastIndexOf(searchValue[, fromIndex])
- searchValue-一個字符串,代表要搜索的值。
- fromIndex-調用字符串中開始搜索的位置。它可以是0到字符串長度之間的任何整數。默認值爲0。
var str1 = new String( "This is string one" );
var index = str1.indexOf( "string" );
console.log("indexOf found String :" + index ); //輸出:indexOf found String :8
var index = str1.indexOf( "one" );
console.log("indexOf found String :" + index );//輸出:indexOf found String :15
var str1 = new String( "This is string one and again string" );
var index = str1.lastIndexOf( "string" );
console.log("lastIndexOf found String :" + index ); //輸出:lastIndexOf found String :29
index = str1.lastIndexOf( "one" );
console.log("lastIndexOf found String :" + index ); //輸出:lastIndexOf found String :15
localeCompare方法返回一個數字,該數字指示參考字符串是按排序順序排在給定字符串之前還是之後還是與給定字符串相同。
語法:string.localeCompare( param ) param-要與字符串對象進行比較的字符串。
返回值
- 0-如果字符串匹配100%。
- 1-不匹配,並且參數值在語言環境排序順序中位於字符串對象的值之前
- 負值-不匹配,並且參數值按本地排序順序位於字符串對象的值之後
var str1 = new String( "This is beautiful string" );
var index = str1.localeCompare( "This is beautiful string");
console.log("localeCompare first :" + index ); //輸出:localeCompare first :0
replace方法查找正則表達式和字符串之間的匹配項,並將匹配的子字符串替換爲新的子字符串。
替換字符串可以包括以下特殊替換模式-
Sr.No | Patterns & Inserts |
---|---|
1 |
$$ 插入“ $”。 |
2 |
$& 插入匹配的子字符串。 |
3 |
$` 插入匹配子字符串之前的字符串部分。 |
4 |
$' 插入匹配的子字符串之後的字符串部分。 |
5 |
$n or $nn 其中n或nn是十進制數字,如果第一個參數是RegExp對象,則插入第n個帶括號的子匹配字符串。 |
語法:string.replace(regexp/substr, newSubStr/function[, flags]);
參數詳細信息
-
regexp-一個RegExp對象。匹配將替換爲參數#2的返回值。
-
SUBSTR -一個字符串,由newSubStr代替。
-
newSubStr-替換從參數#1接收到的子字符串的字符串。
-
函數 -要調用的函數以創建新的子字符串。
-
標誌 -一個包含RegExp標誌的任意組合的字符串:g。
var re = /apples/gi; var str = "Apples are round, and apples are juicy."; var newstr = str.replace(re, "oranges"); console.log(newstr) //輸出:oranges are round, and oranges are juicy. var re = /(\w+)\s(\w+)/; var str = "zara ali"; var newstr = str.replace(re, "$2, $1"); console.log(newstr); //輸出:ali, zara
search方法執行搜索以查找正則表達式與此String對象之間的匹配項。
語法:string.search(regexp);
參數:regexp-正則表達式對象。如果傳遞了非RegExp對象obj,則使用新的RegExp(obj)將其隱式轉換爲RegExp。
如果成功,搜索將返回字符串內的正則表達式的索引。否則,它返回-1。
var re = /apples/gi;
var str = "Apples are round, and apples are juicy.";
if ( str.search(re) == -1 ) {
console.log("Does not contain Apples" );
} else {
console.log("Contains Apples" );
}
//輸出Contains Apples.
slice方法提取字符串的一部分並返回新的字符串。
語法:string.slice( beginslice [, endSlice] );
參數詳細信息
-
beginSlice-從零開始的索引。
-
endSlice-從零開始的索引,終止提取。如果省略,則切片提取到字符串的末尾
返回值
如果成功,slice將返回字符串內的正則表達式的索引。否則,它返回-1。
var str = "Apples are round, and apples are juicy.";
var sliced = str.slice(3, -2);
console.log(sliced); //輸出:les are round, and apples are juic
split方法通過將字符串對象分成子字符串來將String對象拆分爲字符串數組。
語法:string.split([separator][, limit]);
參數詳細信息
-
separator -指定用於分隔字符串的字符。如果省略了分隔符,則返回的數組包含一個由整個字符串組成的元素。
-
limit -整數,指定在拆分的數量限制。
返回值
split方法返回新數組。同樣,當字符串爲空時,split返回一個包含一個空字符串的數組,而不是一個空數組。
var str = "Apples are round, and apples are juicy.";
var splitted = str.split(" ", 3);
console.log(splitted) //輸出:[ 'Apples', 'are', 'round,' ]
substr方法以指定的字符數返回從指定位置開始的字符串中的字符。
語法:string.substr(start[, length]);
參數詳細信息
-
start-開始提取字符的位置(0到1之間的整數,小於字符串的長度)。
-
length -要提取的字符數
注 -如果start爲負,則substr將從字符串末尾開始將其用作字符索引。
返回值
substr()方法根據給定的參數返回新的子字符串。
var str = "Apples are round, and apples are juicy."; console.log("(1,2): " + str.substr(1,2)); //輸出:(1,2): pp console.log("(-2,2): " + str.substr(-2,2)); //輸出:(-2,2): y. console.log("(1): " + str.substr(1)); //輸出:(1): pples are round, and apples are juicy.
subtring方法返回String對象的子集。
語法:string.substring(indexA, [indexB])
參數詳細信息
-
indexA-一個小於字符串長度的0到1之間的整數。
-
indexB-(可選)0到字符串長度之間的整數。
返回值
返回根據給定參數的新字符串。
var str = "Apples are round, and apples are juicy."; console.log("(1,2): " + str.substring(1,2)); //輸出:(1,2): p console.log("(0,10): " + str.substring(0, 10)); //輸出:(0,10): Apples are console.log("(5): " + str.substring(5)); //輸出:(5): s are round, and apples are juicy
toLocaleLowerCase方法用於在遵守當前語言環境的同時將字符串中的字符轉換爲小寫。對於大多數語言,它返回與toLowerCase相同的輸出。
語法:string.toLocaleLowerCase( )
返回值
以當前語言環境返回小寫的字符串。
var str = "Apples are round, and Apples are Juicy.";
console.log(str.toLocaleLowerCase( )); //輸出:apples are round, and apples are juicy.
toLowerCase方法返回轉換爲小寫形式的調用字符串值。
toUpperCase方法返回轉換爲大寫形式的調用字符串值。
語法:string.toLowerCase( ) string.toUpperCase()
var str = "Apples are round, and Apples are Juicy.";
console.log(str.toLowerCase( )) //輸出:apples are round, and apples are juicy. console.log(str.toUpperCase( )); //輸出:APPLES ARE ROUND, AND APPLES ARE JUICY.