Javascript介绍字符串对象{初学者}
一、字符串对象字符串对象用于处理文本(字符串)。
二,构造函数
新字符串(value)构造函数
函数字符串(值)传递函数
三、属性
字符串中字符的长度
var str =新的字符串(真);
document.write(str.length); / /输出7
四。方法
1、chatat()以指定位置的字符在字符串中。
var str =新的字符串(真);
document.write(str.charat(1)); / / B输出
2、chatcodeat()返回一个字符串中指定位置的字符编码。
var str =新的字符串(真);
document.write(str.charcodeat(1)); / /输出98
3、concat()将一个或多个值的字符串。
var str =新的字符串(真);
var str1 =但是;
document.write(str.concat(str1)); / / ABCDEFGHIJK输出
4、IndexOf()用于指定字符串中的字符或字符串的位置搜索。如果你找不到返回1
语法:indexOf(STR)子字符串或字符序列:
IndexOf(STR,开始)STR:串或字符。启动:指定搜索的起始位置
var str =新的字符串(abccba );
document.write(str.indexof(B)); / /输出1
(str.lastindexof document.write(公元前)); / /输出1
使用此方法实现包含效果,确定字符串是否包含另一个字符串:
在window.onload =函数(){
var str1 = 刘备;
VaR str2 =刘备是牛!;
警报(str2.indexof(str1)); / /输出0位
如果(str2.indexof(str1)> 1){
警报()!;
}
{其他
警告()不包含!;
}
}
5、(字符串)返回指定字符串(反)为字符或字符串的位置。如果你不觉得回到1
语法:字符串(STR)乙方:字符串或字符
字符串(STR,开始)STR:串或字符。启动:指定搜索的起始位置
var str =新的字符串(abccba );
document.write(str.lastindexof(B)); / /输出4
6、localecompare()比较在局部定义的顺序串。
var str =abccba ;
(str.localecompare document.write(公元前); / / 1输出)
7、匹配()使用正则表达式执行模式匹配。
8、替换()使用正则表达式执行查找和替换操作。
var str =abccba ;
(str.replace document.write(B
9、搜索()找到一个字符串,该字符串与字符串中的正则表达式匹配。
var str =abccba ;
(str.search document.write(B)); / /输出1
10,切片()返回字符串的一个切片或字符串。如果参数为负数,从后面的数字到前一个数字。不要更改原始字符串。
var str =ABCDEFG;
document.write(str.slice(2)+ ); / / cdefg输出
document.write(STR); / /输出真的可以看到原始字符串不改变。
11、分裂()断开与一个指定的分隔符字符串或正则表达式,并返回一个字符串数组。
var str =ABCDEFG;
var arr = str.split(D);
document.write((arr.join)); / /输出ABC,EFG
12、substr()提取子串的字符串,字符串变量(),它已被抛弃。
13、子串()提取子串的字符串。
语法:子串(开始、结束)从开始到结束,包括开始而不是结束。不改变原始字符串。
var12345678;
document.write(str.substring(1,4)); / /输出234
14、toLowerCase()返回指定字符串的小写复制。
var str =ABCDEF;
document.write(str.tolocalelowercase)(ABCDEF); / /输出
15、toString()返回原字符串值。
var str =ABCDEF;
document.write(str.tostring)(ABCDEF); / /输出
16、toUpperCase()返回一个指定字符串的大写。
var str =ABCDEF;
document.write(str.touppercase)(ABCDEF); / /输出
17、修剪()返回指定字符串的左、后空白的副本。
var str =ABCDEF;
document.write(11 +)str.trim(+ 11 + ); / / 11abcdef11输出
document.write(11 +力量+11); / / 11 ABCDEF输出11
18、价值()返回原字符串值。
var str =ABCDEF;
document.write(str.valueof)(ABCDEF); / /输出
以上是本文的全部内容,希望能对您有所帮助,谢谢您的支持!