JS:1.8字符串(string)对象(length,indexOf(),lastIndexOf(),replace(),match(),toUpperCase(),toLowerCase())

string:字符串(string)对象
JS:1.8.0,JavaScript String(字符串)对象 实例返回顶部
计算字符串的长度
如何使用长度属性来计算字符串的长度。
为字符串添加样式
如何为字符串添加样式。
indexOf() 方法
如何使用 indexOf() 来定位字符串中某一个指定的字符首次出现的位置。
match() 方法
如何使用 match() 来查找字符串中特定的字符,并且如果找到的话,则返回这个字符。
如何替换字符串中的字符 - replace()
如何使用 replace() 方法在字符串中用某些字符替换另一些字符。
JS:1.8.1,length属性返回顶部
复制代码
<html>
<body>

<script type="text/javascript">

var txt="Hello World!"

//输出字符串的长度
document.write(txt.length)

</script>

</body>
</html>
复制代码
JS:1.8.2,indexOf(),lastIndexOf()返回顶部
复制代码
<html>
<body>
<script language="javascript">
var txt="Hello world!";

// indexOf(),lastIndexOf() 来定位字符串中某一个指定的字符首次出现的位置
//若不存在则返回“-1”

document.write(txt+"<br>");

document.write("该字符串的长度为:"+txt.length+" <br>");
document.write("从前往后找,第一位字符'l'它的索引位置为:"+txt.indexOf('l'+"<br>"));
document.write("从后往前查,第一个字符'l'它的索引位置为:"+txt.lastIndexOf('l')+"<br>");
</script>

</body>
</html>
复制代码
JS:1.8.3,replace()返回顶部
复制代码
<html>
<body>

<h3>replace(要替换内容,替换成的内容)</h3>
<script language="javascript">
var txt="某某喜欢王帅!";
document.write("替换前的内容:"+txt+"<br>");
document.write("替换后的内容:"+txt.replace("某某","凤姐"));
</script>
</body>
</html>
复制代码
JS:1.8.4,match()返回顶部
复制代码
<html>
<body>

<script type="text/javascript">

var str="Hello world!"
document.write(str.match("world") + "<br />")
document.write(str.match("World") + "<br />")
document.write(str.match("worlld") + "<br />")
document.write(str.match("world!"))

</script>

</body>
</html>
复制代码
JS:1.8.5,toUpperCase(),toLowCase()返回顶部
复制代码
<html>
<body>
<script language="javascript">
var txt="Hello world!";

document.write(txt+"<br>");
document.write("把所有字符转为大写:"+txt.toUpperCase()+"<br>");
document.write("把所有字符转为小写:"+txt.toLowerCase());
</script>

</body>
</html>
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值