javascript string类常用方法整理

anchor() 创建 HTML 锚。
参数:anchorname 必需。为锚定义名称。
用法:
<script type="text/javascript">
var txt="Hello world!"
document.write(txt.anchor("myanchor"))
</script>
上面的代码将输出为纯粹的 HTML:
<a name="myanchor">Hello world!</a>

 -------------------------------------------------------------------------------------------------------------------

big() 用大号字体显示字符串
small() 使用小字号来显示字符串。
用法:
var txt="Hello World!"
document.write("<p>Big: " + txt.big() + "</p>")
document.write("<p>Small: " + txt.small() + "</p>")

输出如下图:

 -------------------------------------------------------------------------------------------------------------------

bold() 使用粗体显示字符串。
italics() 使用斜体显示字符串

用法:
document.write("<p>Bold: " + txt.bold() + "</p>")
document.write("<p>Italic: " + txt.italics() + "</p>")

输出:

Bold: Hello World!

Italic: Hello World!

 -------------------------------------------------------------------------------------------------------------------

blink() 显示闪动字符串。
fixed() 以打字机文本显示字符串。
strike() 使用删除线来显示字符串。

用法:

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>")

输出:

Blink: Hello World! (does not work in IE)

Fixed: Hello World!

Strike: Hello World!

 -------------------------------------------------------------------------------------------------------------------

fontcolor() 使用指定的颜色来显示字符串。
fontsize() 使用指定的尺寸来显示字符串

用法:

document.write("<p>Fontcolor: " + txt.fontcolor("Red") + "</p>")
document.write("<p>Fontsize: " + txt.fontsize(16) + "</p>")

输出:

Fontcolor: Hello World!

Fontsize: Hello World!

 -------------------------------------------------------------------------------------------------------------------

toLowerCase() 把字符串转换为小写。
toUpperCase() 把字符串转换为大写。

toLocaleLowerCase() 把字符串转换为小写。
toLocaleUpperCase() 把字符串转换为大写。

用法:

document.write("<p>Lowercase: " + txt.toLowerCase() + "</p>")
document.write("<p>Uppercase: " + txt.toUpperCase() + "</p>")

输出:

Lowercase: hello world!

Uppercase: HELLO WORLD!

注:toLowerCase()/toUpperCase()和toLocaleLowerCase() /toLocaleUpperCase()用法相同,

其中,toLowerCase()和toUpperCase()是两个经典的方法,借鉴自java.lang.String中的同名方法。而toLocaleLowerCase()和toLocaleUpperCase()方法则是针对特定地区的实现。

对有些地区来说,针对地区的方法与其通用方法得到的结果相同,但少数语言(如土耳其语言)会为Unicode大小写转换应用特殊的规则,这时候就必须使用针对地区的方法来保证实现正确的转换

 -------------------------------------------------------------------------------------------------------------------

sub() 把字符串显示为下标。
sup() 把字符串显示为上标。

用法:

document.write("<p>Subscript: " + txt.sub() + "</p>")
document.write("<p>Superscript: " + txt.sup() + "</p>")

输出:

Subscript: Hello World!

Superscript: Hello World!

 -------------------------------------------------------------------------------------------------------------------
charAt(index)返回指定索引位置处的字符。如果超出有效范围的索引值返回空字符串
index:想得到的字符的基于零的索引。有效值是0与字符串长度减一之间的值。 

用法:

var str = "ABC"; 
str.charAt(1); 
输出:B 

-------------------------------------------------------------------------------------------------------------------
charCodeAt() 返回在指定的位置的字符的 Unicode 编码。

用法:

var str = "ABC"; 
str.charCodeAt(0); 

输出:65

说明:位置0处的字符是A, A的Unicode编码为65。所以输出为65

-------------------------------------------------------------------------------------------------------------------

fromCharCode() 从字符编码创建一个字符串。

用法:

document.write(String.fromCharCode(72,69,76,76,79))
document.write("<br />")
document.write(String.fromCharCode(65,66,67))

输出:

HELLO
ABC
---------------------------------------------------------------------------------------------------------------

concat() 方法用于连接两个或多个字符串

用法:

var str1="Hello "
var str2="world!"
document.write(str1.concat(str2))

输出:

Hello world!
注:一般情况下使用“+”来进行字符串连接更为简洁。
--------------------------------------------------------------------------------------------------
 

indexOf() 检索字符串。
lastIndexOf() 从后向前搜索字符串。
link() 将字符串显示为链接。
localeCompare() 用本地特定的顺序来比较两个字符串。
match() 找到一个或多个正则表达式的匹配。
replace() 替换与正则表达式匹配的子串。
search() 检索与正则表达式相匹配的值。
slice() 提取字符串的片断,并在新的字符串中返回被提取的部分。
split() 把字符串分割为字符串数组。
substr() 从起始索引号提取字符串中指定数目的字符。
substring() 提取字符串中两个指定的索引号之间的字符。
toSource() 代表对象的源代码。
toString() 返回字符串。
valueOf() 返回某个字符串对象的原始值。

posted @ 2017-05-09 10:17  云里有棵树  阅读(238)  评论(0编辑  收藏  举报