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

JavaScript 字符串方法

时间:2017-09-18 22:29:41      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:set   body   个数   基于   位置   使用   ring   理解   tin   

字符方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>字符方法</title>
</head>
<body>
<script type="text/javascript">
/*
charAt方法和charCodeAt方法都接收一个参数,基于0的字符位置
charAt方法是以单字符字符串的形式返回给定位置的那个字符
charCodeAt方法获取到的不是字符而是字符编码
*/
var str="hello world";
console.log(str.charAt(1));//e
console.log(str.charCodeAt(1));//101
//还可以使用方括号加数字索引来访问字符串中特定的字符
console.log(str[1]);//e
</script>
</body>
</html>
字符串操作方法
concat方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>concat方法</title>
</head>
<body>
<script type="text/javascript">
var str="hello ";
var res=str.concat("world");
console.log(res);//hello world
console.log(str);//hello 这说明原来字符串的值没有改变
var res1=str.concat("nihao","!");
console.log(res1);//hello nihao! 说明concat方法可以接收任意多个参数
//虽然concat方法是专门用来拼接字符串的,但是实践中我们使用最多的还是加操作符+,因为其简易便行
</script>
</body>
</html>
slice方法、substring方法、substr方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>字符串操作方法</title>
</head>
<body>
<script type="text/javascript">
/*
slice方法:第一个参数指定子字符串开始位置,第二个参数表示子字符串最后一个字符后面的位置
substring方法:第一个参数指定子字符串开始位置,第二个参数表示子字符串最后一个字符后面的位置
substr方法:第一个参数指定子字符串开始位置,第二个参数表示返回的字符个数
这三个方法都会返回被操作字符串的一个子字符串,都接收一或两个参数
如果没有给这些方法传递第二个参数,则将字符串的长度作为结束位置。这些方法也不会修改字符串本身,只是返回一个基本类型的字符串值
*/
var str="hello world";
console.log(str.slice(3));//lo world
console.log(str.substring(3));//lo world
console.log(str.substr(3));//lo world
console.log(str.slice(3,7));//lo w 7表示子字符串最后一个字符后面的位置 简单理解就是包含头不包含尾
console.log(str.substring(3,7));//lo w
console.log(str.substr(3,7));//lo worl 7表示返回7个字符

console.log(str.slice(3,-4));//lo w -4+11=7表示子字符串最后一个字符后面的位置 简单理解就是包含头不包含尾
console.log(str.substring(3,-4));//hel 会转换为console.log(str.substring(3,0));
//此外由于这个方法会将较小数作为开始位置,较大数作为结束位置,所以相当于调用console.log(str.substring(0,3));
console.log(str.substr(3,-4));//""空字符串
console.log(str.substring(3,0));
</script>
</body>
</html>
字符串位置方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>字符串位置方法</title>
</head>
<body>
<script type="text/javascript">
/*
indexOf方法和lastIndexOf方法都是从一个字符串中搜索给定的子字符串,然后返回子字符串的位置,如果没有找到,则返回-1
indexOf方法是从字符串的开头向后搜索子字符串,lastIndexOf方法正好相反
这两个方法都可以接收两个参数:要查找的子字符串和查找的位置
*/
var str="hello world";
console.log(str.indexOf("o"));//4
console.log(str.lastIndexOf("o"));//7
console.log(str.indexOf("o",6));//7
console.log(str.lastIndexOf("o",6));//4
</script>
</body>
</html>
trim方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>trim方法</title>
</head>
<body>
<script type="text/javascript">
/*
trim方法用来删除字符串前后的空格
*/
var str=" hello world ";
console.log(‘(‘+str.trim()+‘)‘);//(hello world)
console.log(‘(‘+str+‘)‘);//( hello world )
</script>
</body>
</html>

JavaScript 字符串方法

标签:set   body   个数   基于   位置   使用   ring   理解   tin   

原文地址:http://www.cnblogs.com/suntao12138/p/7544903.html

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