您的位置:首页 > Web前端 > JavaScript

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

2015-05-22 15:54 746 查看
string:字符串(string)对象
JavaScript String(字符串)对象 实例
length属性
indexOf(),lastIndexOf()
replace()
match()
toUpperCase(),toLowCase()

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>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐