Javascript字符串处理函数
var str = "AbcD"
//转换为小写,结果:abcd
alert(str.toLowerCase());
//转换为大写,结果:ABCD
alert(str.toUpperCase());
//转换为小写,结果:abcd
alert(str.toLowerCase());
//转换为大写,结果:ABCD
alert(str.toUpperCase());
//从字符串中截取第x到第y位(索引从0开始),结果:"34"
var s = "1234567890"
var x = 2;
var y = 4;
alert(s.substring(x, y));
var s = "1234567890"
var x = 2;
var y = 4;
alert(s.substring(x, y));
代码
//返回str2在str1中第一个字符所在的位置(索引从0开始),如不存在返回-1,结果:3
var str1 = "1234567890456";
var str2 = "456";
alert(str1.indexOf(str2));
//返回str2在str1中最后一个字符所在的位置(索引从0开始),如不存在返回-1,结果:10
var str1 = "1234567890456";
var str2 = "456";
alert(str1.lastIndexOf(str2));
//返回str2在str1中第一个字符所在的位置(索引从0开始),如不存在返回-1,结果:3
var str1 = "1234567890456";
var str2 = "456";
alert(str1.indexOf(str2));
//返回str2在str1中最后一个字符所在的位置(索引从0开始),如不存在返回-1,结果:10
var str1 = "1234567890456";
var str2 = "456";
alert(str1.lastIndexOf(str2));
//将数组转换为字符串
var arr = ["a","b","c"];
alert(arr.join("|"));
//输出结果:a|b|c
//将字符串转换为数组
var arrStr = "a,b,c";
var arr = arrStr.split(",");
//结果:arr = ["a","b","c"];
var arr = ["a","b","c"];
alert(arr.join("|"));
//输出结果:a|b|c
//将字符串转换为数组
var arrStr = "a,b,c";
var arr = arrStr.split(",");
//结果:arr = ["a","b","c"];