js中字符串的使用

javascript中String的使用
String对象
String 对象用于处理文本(字符串)。字符串的长度不可更改,所有的方法不能修改
内容

String对象的创建
1:var str = new String(“hello,world”);//构造器
2:var str = “hello,world”

“h_e_l_l_o_,_w_o_r_l_d”
1:big()
用大号字体显示字符串。

语法:str.big()
运行:
var str=“Hello world!”
document.write(str.big())
输出:Hello:Hello world!

2:small()
字符串显示为小号字。

语法:str.small()
运行:

var str="Hello world!"
document.write(str.small())

输出:Hello:Hello world!

3:toLowserCase()
字符串转换为小写。

语法:str.toLowserCase()
运行:

var str="Hello world!"
document.write(str.toLowserCase())

输出:hello world!
4:toLocaleLowerCase()
按照本地方式把字符串转换为小写

语法:str.toLocaleLowerCase()
运行:

var str="Hello world!"
document.write(str.toLocaleLowerCase())

输出:hello world!
5:toUpperCase()
字符串转换为大写

语法:str.toUpperCase()
运行:

var str="Hello world!"
document.write(str.toUpperCase())

输出:HELLO WORLD
6:toLocalUpperCase()
按照本地方式把字符串转换为大写

语法:str.toLocalUpperCase()
运行:

var str="Hello world!"
document.write(str.toLocalUpperCase())

输出:HELLO WORLD
7:sub()
把字符串显示为下标。

语法:str.sub()
运行:

var str="Hello world!"
document.write("sub:",str.sub())

输出:sub:Hello world!

8:sup()
把字符串显示为上标。

语法:str.sub()
运行:

var str="Hello world!"
document.write("sup:",str.sup())

输出:sup:Hello world!
注:上下标这里每做特殊显示,担待~~
9:bold()
把字符串显示为粗体。

语法:str.bold()
运行:

var str="Hello world!"
document.write("bold:",str.bold())

输出:bold:Hello world!

10:italics()
把字符串显示为斜体。
语法:str.italics()
运行:

var str="Hello world!"
document.write("italics:",str.italics())

输出:italics:Hello world!

11:strike()
显示加删除线的字符串。
语法:str.strike()
运行:

var str="Hello world!"
document.write("strike:",str.strike())

输出:strike:Hello world!

12:blink()
用于显示闪动的字符串。
语法:str.blink()
运行:

var str="Hello world!"
document.write("blink:",str.blink())

输出:blink:Hello world!

13:link()
用于把字符串显示为超链接。
语法:str.link()
运行:

var str="百度"
document.write(str.link("http://baidu.com"))

输出:百度

14:anchor()
用于创建 HTML 锚。
语法:str.anchor()
运行:

var str="anchor"
document.write(str.anchor("anchor"))

输出:anchor

15:fixed()
以打印字体显示文本
语法:str.fixed()
运行:

var str="hello,world"
document.write(str.fixed())

输出:hello,world
16:fontcolor()
按照指定的颜色来显示字符串。
语法:str.fontcolor(“red”)
运行:

var str="hello,world";
document.write(str.fontcolor("red"))

输出:fontcolor:Hello world!

17:fontsize(n)
用于按照指定的尺寸来显示字符串。
1<= n <= 7
语法:str.fontsize(“red”)
运行:

var str="hello,world";
document.write(str.fontsize("7"))

输出:fontsize:Hello world!

18:indexOf()
返回某个指定的字符串值在字符串中首次出现的位置。
语法:str.indexOf(value,index)
value:检索的字符串
index:检索的开始位置
indexOf() 方法对大小写敏感!
如果要检索的字符串值没有出现,则该方法返回 -1。
运行:

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

输出:0 -1 6
19:lastIndexOf()
返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。
语法:str.lastIndexOf(value,index)
value:检索的字符串
index:检索的开始位置
lastIndexOf() 方法对大小写敏感!
如果要检索的字符串值没有出现,则该方法返回 -1。
运行:

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

输出:0 -1 6
20:charAt()
返回指定位置的字符
语法:str.charAt(index)
运行:

var str = "hello,world";
document.write(str.charAt(1));

输出:e
21:charCodeAt()
返回指定位置的字符的 Unicode 编码。这个返回值是 0 - 65535 之间的整数。
语法:str.charCodeAt(index)
输入:

var str = "hello,world";
document.write(str.charCodeAt(1));

输出:101
22:fromCharCode()
可接受一个指定的 Unicode 值,然后返回一个字符串。
语法:String.fromCharCode(numX,numX,…,numX)
输入:

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

输出:6HELLO ABC
23:match()
在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
语法:
str.match(searchvalue)
str.match(regexp)
存在,返回值
不存在,返回null
输入:

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!"))
var str1="1 plus 2 equal 3"
document.write(str1.match(/\d+/g))

输出:world null null world!
1,2,3
24:repalce()
在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
语法:str.replace(regexp/str,str)
输入:

var str="hello world world"
document.write(str.replace(/world/, "js"))
document.write(str.replace(/world/g, "js"))

输出:hello js world hello js js
25:search()
用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。
存在,返回索引位置
不存在,返回-1
语法:str.search(regexp)
输入:

var str="hello world world"
document.write(str.search(/world/))

输出:6
26:substr()
可在字符串中抽取从 start 下标开始的指定数目的字符。
语法:str.substr(start.length)
输入:

var str = "hello world";
document.write(str.substr(1,3)+"<br/>")

输出:ell
27:substring()
提取字符串中两个指定的索引号之间的字符。
语法:str.substring(start,end)
输入:

var str = "hello world";
document.write(str.substring(1,3)+"<br/>")

输出:el
28:concat()
连接两个或多个字符串。
语法:
var str1 = “hello”;
var str2 = “world”;
str.concat(str2)
输入:

var str1 = "hello";
var str2 = "world";
document.write(str.concat(str2)+"<br/>")

输出:hello world
29:localCompare()
用本地特定的顺序来比较两个字符串。

30:slice()
提取字符串的片断,并在新的字符串中返回被提取的部分。
语法:
str.slice(start,end)
输入:

var str = "hello world";
document.write(str.slice(1)+"<br/>")
document.write(str.slice(1,2)+"<br/>")

输出:ello world e
31:split()
把一个字符串分割成字符串数组。
语法:
str.split(separator,size)
separator:字符串或正则表达式,从该参数指定的地方分割 str。
size:生成数组长度
输入:

var str = "hello world";
document.write(str.split("")+"<br/>")
document.write(str.split(" ")+"<br/>")
document.write(str.split("e")+"<br/>")
document.write(str.split("",3)+"<br/>")

输出:
h,e,l,l,o, ,w,o,r,l,d
hello,world
h,llo world
h,e,l
32:toSource()
代表对象的源代码。

33:toString()
返回一个字符串。

语法:str.toString()
输入:

var str = "hello world"
document.write(str.valueOf())

输出:
hello world
34:valueOf()
返回某个字符串对象的原始值。
语法:str.valueOf()
输入:

var str = "hello world"
document.write(str.valueOf())

输出:hello world

注:本文中有部分特殊效果没有展示出来~~望大家谅解!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值