js字符串操作

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>字符串练习</title>
</head>
<body>
<script>
//字符串的长度
var hello = 'helloword';
console.log(hello.length)

//indexof定位字符串中某一个指定的字符首次出现的位置
var hellos = 'helloword';
console.log(hellos.indexOf('l'));

//match() 来查找字符串中特定的字符,并且如果找到的话,则返回这个字符 没找到 返回null
var s = 'helloword';
alert(s.match('b'))

//替换字符串 (不会改变原来的字符串)
var ss = 'helloword'
ss.replace(/hello/,'改变');
console.log(ss.replace(/hello/,'改变'));

//把字符串分割成数组 (split()里面不占数组内容)
var string1 = '这是字符串1';
string1.split('是');
console.log(string1.split('是'));

//把字符串显示为下标
var string2 = '这是字符串2';
console.log(string2.sub());
document.write('下标:'+string2.sub())

//把字符串显示为下标
var string3 = '这是字符串2';
console.log(string3.sub());
document.write('上标:'+string3.sup());


//字符串大写
var string4= 'DaXie';
console.log(string4.toLocaleUpperCase());

//字符串小写
var string5 = 'XiaoXie'
console.log(string5.toLocaleLowerCase());

//从字符串中指定下标提取字符
var string6 = '这是字符串6';
console.log(string6.substr(3))

//字符串颜色、
var string7 = '改变我的颜色';
document.write(string7.fontcolor('red'));

//连接2个字符
console.log(string7.concat(string6));

//返回在指定位置的字符
var string8 = '指定位置的字符';
console.log(string8.charAt(2));

//提取字符串
var string9 = '提取字符串'
console.log(string9.slice(0,2));



</script>
</body>
</html>

posted @ 2017-11-20 10:58  松前月下  阅读(117)  评论(0编辑  收藏  举报