【JavaScript高级程序设计】7、基本包装类型(2)
JavaScript中的字符串中的字符就算是双字符的,在求length的时候还是算一个字符
1、字符操作
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<script type="text/javascript">
var stringValue = "hello world";
//从0开始计数
alert(stringValue.charAt(1)); //e
//得到字符的编码101,就是ascii编码
alert(stringValue.charCodeAt(1));
//直接通过索引,但是ie7不支持,ie11是支持的
alert(stringValue[1]);
</script>
<body>
</body>
</html>
结果:



2、字符串操作方法
对于字符串操作有concat方法拼接字符串
而后:slice,substring,substr三个方法作用各不相同,具体如何不同,请看代码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<script type="text/javascript">
//使用concat进行拼接字符串
var stringvalue2 = "hello ";
var result = stringvalue2.concat("world");
alert(result);
//但是原来的字符串不改变
alert(stringvalue2);
//使用+来拼接字符串
alert("hello " + "world" + "!");
</script>
<body>
</body>
</html>
结果:



其余三个方法
slice,substring,substr三个方法
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<script type="text/javascript">
//这三个函数都可以接受2个参数,第一个参数指定字符串开始的位置,第二个参数slice和substring
//指定字符结束串最后一个字符的后面一个位置,substr指定的是字符串第二个参数是子字符串的个数
var stringValue = "hello world";
//从第三个开始到末尾
alert("一个参数hello world 第三个开始 ==》slice : " + stringValue.slice(3) + "\tsubstring : " + stringValue.substring(3) + "\tsubstr : " + stringValue.substr(3));
//前两个是从第三个到第7个,最后一个是7个字符
alert("二个参数hello world (3,7) ==》slice : " + stringValue.slice(3, 7) + "\tsubstring : " + stringValue.substring(3, 7) + "\tsubstr : " + stringValue.substr(3, 7));
//如果是负数
//这里注意,如果是substring,substr的话是吧负数(substr是第二个参数,前者是不论第几个参数)转换为0的,其他的是吧负数和字符串总长度和负数相加
alert("一个参数hello world 第-3个开始 ==》slice : " + stringValue.slice(3) + "\tsubstring : " + stringValue.substring(3) + "\tsubstr : " + stringValue.substr(3));
//这里还需要注意滴二个,这里是-4转为0之后,substring是吧小的作为第一个参数,打的作为第二个参数的
alert("二个参数hello world (3,-4) ==》slice : " + stringValue.slice(3, -4) + "\tsubstring : " + stringValue.substring(3, -4) + "\tsubstr : " + stringValue.substr(3, -4));
</script>
<body>
</body>
</html>
结果:




总结:
slice:第一个参数是子字符串的起始位置,第二个参数是最后一个位置的下一个位置,如果有负数,第一个和第二个参数都是和字符总长相加得到的正数
substring:两个参数,小的是子字符串的起始位置,大的那个个参数是最后一个位置的下一个位置,如果有负数,第一个和第二个都是转换为0
substr:第一个参数是子字符串的起始位置,第二个参数是子字符串的长度,如果有负数,那么第一个参数的负数是用来加上字符总长,第二个参数转换为0
3、字符串位置
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script type="text/javascript">
//找到字符串中所有的e
var stringValue = "Lorem ipsum dolor sit amet, consectetur adipisicing elit";
var positions = new Array();
var pos = stringValue.lastIndexOf("e");
while(pos > -1)
{
positions.push(pos); //压栈
//在当前位置向前遍历
pos = stringValue.lastIndexOf("e", pos - 1);
}
alert(positions.join("$"));
</script>
</head>
<body>
</body>
</html>
结果:

4、trim
删除字符串前面和后缀的所有空字符
5、字符串的模式匹配
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script type="text/javascript">
//吧所有的html中的额<,>,&,\转换成相应的字符
function htmlEscape(text)
{
//吧所有的html中的相应字符转换为相应的标签,replace参数可以是一个function函数
//这个函数中1、replace匹配到的字符,2、匹配到的位置,3、用来匹配的原字符串
return text.replace(/[<>"&]/g, function(match, pos, originalText)
{
switch(match)
{
case "<":
return "<";
case ">":
return ">";
case "&":
return "&";
case "\"":
return """;
}
});
}
var xmldata = "<?xml version='1.0' encoding='UTF-8'?> <Info> <SysCode>ORGMAINCHG</SysCode>"
+
"<ServName>createOrgMainChg</ServName> <SheetTitle>测试20160321-02</SheetTitle> <SheetDesc>TEST!!!!</SheetDesc>"
+
"<ChgAreaCode>350000</ChgAreaCode> <AttachList> <Attach> <AttachName>文档1</AttachName> <AttachUrl>文档链接</AttachUrl>"
+
"</Attach> <Attach> <AttachName>文档2</AttachName> <AttachUrl>asaasd</AttachUrl> </Attach> <Attach> <AttachName>文档3</AttachName>"
+
"<AttachUrl>asdasdas</AttachUrl> </Attach> </AttachList> </Info>";
alert(htmlEscape(xmldata));
</script>
</head>
<body>
</body>
</html>
结果:


浙公网安备 33010602011771号