javaScript的String.prototype的所有方法

js的string的所有方法
formCharCode:
String.formCharCode("55");
>>>>>>"7"
unicode码返回对应的字符串。
charAt:
var str="hello world";
str.charAt(0);
 
>>>>>"h";
通过索引返回对应的字符串,范围为(0~string.length-1);当索引值不在这个范围时,返回一个空字符串。
charCodeAt:
var str="hello world";
str.charCodeAt(0);
>>>>>104(Number);
通过索引返回对应字符的unicode码,效果和用法和charAt一样,范围为(0~65535),当索引不在范围内时,结果返回NaN;
codePointAt:
通过索引返回对应字符的unicode码,用法和charCodeAt一样,当索引不在范围时,结果返回undefined;
concat:
var str="hello",
    str2="world";
var str3=str.concat(str2);
>>>>>"helloworld";
合并两个字符串
endsWith:
var str="hello world";
str.endsWith("d");
>>>>>true
是否以查询的字符串结尾,返回值为布尔值。
includes:
var str="hello world";
str.includes("he");
>>>>>>true
str.includes("hl");
>>>>>>false
查询是否含有子字符串,返回值为布尔值。
indexOf:
str="hello world";
str.indexOf("l");
>>>>>>2
str.indexOf("z");
>>>>>>-1
查询字符串,返回第一次查询到的索引值,如果没有查询的值,返回-1.
lastIndexOf:
var str="hello world";
str.lastIndexOf("l");
>>>>>>9
str.laseIndexOf("ll");
>>>>>>2
查询字符串,返回最后一次查询到的索引值,如果没有查询的值,返回-1.
localeCompare:
规定该函数采用底层操作系统提供的排序规则。
match:
var str="hello world";
str.match('hello');
>>>>>>"hello"
str.match(/l+/g);
>>>>>>['ll','l']
var str="<input value={wwwww} type={222222} />";
var arr=str.match(/{(\w+)}/);
>>>>>>['{wwwww}','wwwww'];
for(var i in arr)console.log(i);
>>>>>>0,1,index,input
参数有两种形式,可以是字符串也可以是正则对象。返回查询到的字符串本身,如果没有规定查询的字符串,则返回null。
加入正则表达式中没有全局匹配的话,返回一个匹配到的字符串,和子查询到的字符串组成的数组,该数组返回一个查询的
索引值。和被查询的字符串对象。
normalize:
repeat:
var str="lili";
str.repeat(2);
>>>>>>"lililili";
参数为复制的次数,当参数小于零时会报错;
replace:
var str="hello world!";
str.replace("l",'o');
>>>>>>"heolo world!";
str.replace(/l/g,"o");
>>>>>>"heooo worod";
str.replace(/l/g,function(){
   return "o";
})
>>>>>>"heooo worod";
replace可接受两个参数,第二个参数如果不选择,则会将目标字符串替换为undefind。第一个参数可以为字符串,也可以为正则对象。第二个参数可以是字符串,也可以是一个有返回值的函数。
search:
 
var str="123456789.abcde";
console.log(str.search("."));//0 因为正则.匹配\n以外任意字符
console.log(str.indexOf("."));//9 只匹配字符串.
console.log(str.search("\\."));//9 
console.log(str.indexOf("\\."));//-1 匹配\. 所以不存在
console.log(str.search(/\./));  //9 正则匹配后.字符
console.log(str.indexOf(/\./)); //-1  相当于匹配'/\./' 所以不存在
search是强制正则匹配
slice:
var str="hello world";
str.slice(0);
>>>>>>"hello world"
str.slice(0,-1);
>>>>>>"hello worl"
str.slice(2,1);
>>>>>>""
String.prototype.slice(star,end);两个参数结束参数为可选参数,截取的字符串不包括结束参数的下标。结束参数可以为负数,表示从字符串末位截取。但开始参数在结束参数后面则返回一个空字符串。
split:
var str="hello world";
str.split("");
>>>>>>["h","e","l","l","o"," ","w","o","r","l","d"]
str.split(/\s+/,1);
>>>>>>["hello"]
String.prototype.split();两个参数,第二个参数为可选参数。表示要返回数值的长度,第一个参数可以是字符串也可以是正则表达式。表示已该字符串为分割。
substring:
 
var str="hello world";
str.substring(0,5);
>>>>>>"hello";
str.substring(0);
>>>>>>"hello world";
String.prototype.subString();接受两个参数,第二个为可选参数。如果没有会截取全部的字符串。用法和slice一样,只是第二个参数不能为负数。
substr:
var str="hello world";
str.sbustr(0,5);
>>>>>>"hello"
str.substr(-1,5);
>>>>>>"world"
str.substr(0);
>>>>>>"hello world"
String.prototype.substr(index,length);可接受两个参数,第一个为起点下标,第二个为长度。第一个参数可以是负值。当第二个参数为空时,会截取到字符串的结尾。
startsWith:
var str="hello world";
str.startsWith("hello");
>>>>>>true;
和endsWith用法一样,以什么为开头。返回值为布尔值。
toLowerCase:
把字符串变成小写。
toLocaleLowerCase:
toUppercase:
把字符串变成大写。
toLocaleUpperCase:
trim:
去掉首尾空格。
trimRight:
去掉右边空格。
trimLeft:
去掉左边空格。
link:
anchor:
fontcolor:
fontsize:
big:
blink:
bold:
fixed:
italics:
small:
strike:
sub:
把字符串变为下标。
sup:
把字符串变成上标

转载于:https://www.cnblogs.com/chairs/p/6985597.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值