码迷,mamicode.com
首页 > 编程语言 > 详细

javascript基础之字符串方法

时间:2017-07-21 01:33:13      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:字符串   indexof   trim   转换   编码   replace   star   ...   bcd   


1:属性length
就是获取字符串的长度
注意:中文、数字、英语字母、空格,都是1个长度
eg:
"快乐大本营 oh".length
//8
var str = ‘abc‘.length;
//3
2: 属性prototype
给对象(String)添加属性或方法,并且添加的方法或属性在所有的实例上都是通用的
eg:
String.prototype.trim = function(){
return this.replace(/(^\s*)|(\s*$)/g, ""); 
}
3:concat
连接两个或多个字符串
原字符串没有更改,返回一个新字符串
stringObject.concat(stringX,stringX,...,stringX);
var str1="Hello "
var str2="world!"
var newstr=str1.concat(str2);
console.log(str1);
console.log(str2);
console.log(newstr);
//Hello
//world!
//Hello world!
4:charAt
返回指定位置的字符,从0开始。如果index是个不存在的数字,返回一个空字符串。
stringObject.charAt(index)
eg:
"abcdef".charAt(1);
//"b"
5:charCodeAt
返回指定位置的字符的 Unicode 编码,如果index是个不存在的数字,返回一个NaN。
stringObject.charCodeAt(index)
eg:
var str="Hello";
console.log(str.charCodeAt(2))
//108
5:slice
方法可截取字符串的某个部分,并以新的字符串返回被截取的部分。
stringObject.slice(start,end)
从start下标开始,不包括end
end没有就是提取到最后。
eg:
var str="Hello happy world!"
var newstr=str.slice(6);
console.log(newstr);
console.log(str);
//happy world!
//Hello happy world!
6:subString
提取字符串中,介于两个指定下标之间的字符
从start下标开始,不包括end
注意:start 与 stop 相等,返回的一个空串。
start 比 stop 大,先交换这两个参数,再去提取字符串。
stringObject.substring(start,stop)
var str="Hello world!";
var newstr=str.substring(3);
console.log(newstr);
console.log(str);
//lo world!
//Hello world!
eg:
var str="Hello world!"
var newstr=str.substring(7,3);
console.log(newstr);
console.log(str);
//lo w
//Hello world!
7:subStr
在字符串中抽取从 start 下标开始的指定数目的字符。
stringObject.substr(start,length)
eg:
var str="Hello world!"
var newstr=str.substr(3);
console.log(newstr);
console.log(str);
//lo world!
//Hello world!
8:indexOf
某个指定的字符串值searchvalue在字符串中首次出现的位置。
stringObject.indexOf(searchvalue,fromindex)
fromindex指的是字符串中开始检索的位置
检索的字符串值没有出现,返回 -1。
var str="Hello world!"
var newstr=str.indexOf(‘ll‘);
console.log(newstr);
//2
9:lastIndexOf
某个指定的字符串值searchvalue在字符串中最后出现的位置。
stringObject.lastIndexOf(searchvalue,fromindex)
var str="Hello world!"
var newstr=str.lastIndexOf(‘w‘);
console.log(newstr);
//6
10:search
获取字符在字符串中的位置
stringObject.search(regexp)
未找到任何匹配的子串,则返回 -1。
不执行全局匹配
var str="Hello world!"
var newstr=str.search(/world/);
console.log(newstr);
//6
把一个字符串分割成字符串数组。
注意‘‘和‘ ‘;
var str="Hello world"
var strarr=str.split(" ");
console.log(strarr);
//["Hello", "world"]
var str="Hello,world"
var strarr=str.split(",");
console.log(strarr);
//["Hello", "world"]
var strarr="hello".split("");
console.log(strarr);
//["h", "e", "l", "l", "o"]
11:replace
字符串替换
eg:
"abcd".replace("a","0");
//"0bcd"
eg:
var str="Hello world!"
console.log(str.replace(/world/, "W"))
//Hello W!
12:toLowerCase
把字符串转换为小写
stringObject.toLowerCase()
原字符串不变,返回的是个字符串
var str="Hello World!"
var newstr=str.toLowerCase();
console.log(str);
console.log(newstr);
//Hello World!
//hello world!
13:toUpperCase()
把字符串转换为大写
stringObject.toUpperCase()
原字符串不变,返回的是个字符串
var str="Hello World!"
var newstr=str.toUpperCase();
console.log(str);
console.log(newstr);
//Hello World!
//HELLO WORLD!

javascript基础之字符串方法

标签:字符串   indexof   trim   转换   编码   replace   star   ...   bcd   

原文地址:http://www.cnblogs.com/kelly2017/p/7215467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!