整理一下對string的操作,方便之後的使用。
大體上我把string的操作分爲以下幾類:
- string的變形
- string提取分離(包含正則表達式)
- string和其他類型的轉化
暫時想到這三類,以後補充
string的變形
string的變形,實際上是指string在dom在不同的展現形式,例如放大變色。
實際代碼如下:
<script type="text/javascript">
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() + " (目前只有 Firefox 和 Opera 瀏覽器支持)</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>")
</script>
上圖是以上代碼的效果圖,可以十分清晰的看出在dom中str被對應的標籤包圍,實際上也是如此 我們用txt.big()
來實驗一下。
$("#t").html(txt.big());//成功
console.log(txt.big());//<big>Hello World!</big>
這些方法的原理就是使用對應的標籤包圍string之後生成一個新的string對象。
字符串的大小寫轉換
toUpperCase() 把一個字符串全部轉換爲大寫。
toLowerCase() 把一個字符串全部轉換爲小寫。
去除空格
str.trim() && str.strimLeft() && str.strimRight()
去除字符串首位的空格,去除開始的空格,去除結尾空格。