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.