字符串对象

来源:互联网 发布:樱井知香番号封面 编辑:程序博客网 时间:2024/05/22 17:27

对一直字符串进行操作

var str="Life is a foreign language;all men mispronounce it";

测试长度

str.length;

为字符串添加样式

var txt="Hello World!"document.write("<p>Big: " + txt.big() + "</p>")document.write("<p>Small: " + txt.small() + "</p>")document.write("<p>Bold: " + txt.bold() + "</p>")document.write("<p>Italic: " + txt.italics() + "</p>")document.write("<p>Blink: " + txt.blink() + " (does not work in IE)</p>")//在很多浏览器中不可用,闪烁document.write("<p>Fixed: " + txt.fixed() + "</p>")//打字机字体document.write("<p>Strike: " + txt.strike() + "</p>")//加下划线document.write("<p>Fontcolor: " + txt.fontcolor("Red") + "</p>")//字体颜色有些改变document.write("<p>Fontsize: " + txt.fontsize(16) + "</p>")document.write("<p>Lowercase: " + txt.toLowerCase() + "</p>")document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>")document.write("<p>Subscript: " + txt.sub() + "</p>")document.write("<p>Superscript: " + txt.sup() + "</p>")document.write("<p>Link: " + txt.link("http://www.w3school.com.cn") + "</p>")

自认为不太熟的

document.write("<p>Fontcolor: " + txt.fontcolor("Red") + "</p>")//字体颜色有些改变document.write("<p>Subscript: " + txt.sub() + "</p>")//把字符串显示为下标document.write("<p>Superscript: " + txt.sup() + "</p>")//把字符串显示为上标document.write("<p>Link: " + txt.link("http://www.w3school.com.cn") + "</p>")

在添加链接时 应注意添加的是URL

indexOf() 方法
如何使用 indexOf() 来定位字符串中某一个指定的字符首次出现的位置。

document.write(txt.indexOf("Life")+"<br/>");document.write(txt.indexOf("it") + "<br />")document.write(txt.indexOf("it"));

match() 方法
如何使用 match() 来查找字符串中特定的字符,并且如果找到的话,则返回这个字符。

document.write(txt.match("men")+"<br/>");document.write(txt.match("gilr"));

replace()方法

alert(txt.replace(/men/,"women"));

详情可见http://www.w3school.com.cn/jsref/jsref_obj_string.asp

原创粉丝点击