字符窜
在js中所有的单引号或者双引号—字符窜
创建字符窜:
1.使用new
var str=new String("hello");
alert(str); //答案:hello
var str=new String(true);
alert(typeof str); //答案:object
2、new可以省略
不用new
var str=String(true);
alert(typeof str); //答案:string
3.使用常量创建(一般use这个)
var str="hello";
alert(str); //答案:hello
字符窜的属性:length:
返回的是当前字符窜中字符的个数
//length长度
var str="hello";
alert(str.length); //结果:5
访问字符窜中的字符
1.charAt()
格式:字符窜.charAt(下标)
返回值:对应下标的字符
注意:我们可以通过字符下标去访问该字
var str="hello";
alert(str.charAt(1)); //结果:e
alert(str[1]); //结果:e(通过字符下标去访问该字)
2.ECMAscript中的字符窜是不变的,
也就是说,字符窜一旦创建,
他们的值就不能改变。要改变某个变量保存的字符窜,
首先要销毁原来的字符窜,
然后再用另一个包含新值得字符窜填充改变量
var str="hello";
str[1]="x";
alert(str);//结果:hello
str="hxllo";
alert(str);//结果:hxllo
3."+"号可以作为字符拼接符存在
var str="hello"+"world";
alert(str); //结果:helloworld
方法
方法:在document.write()中使用
big() 用大号字体显示字符窜
blink() 显示闪动字符窜(IE下无效)
bold() 用粗体显示字符窜
fixed() 用打字机文本显示字符窜
strike() 用删除线显示字符窜
fontcolor() 用指定颜色显示字符窜
fontsize() 用指定尺寸显示字符窜
link() 把字`在这里插入代码片`符窜显示为链接
sub() 把字符窜显示为下标
sup() 把字符窜显示为上标
document.write("hello".sub().bold()+"world".sup());
hello加粗和下标
world上标
字符窜方法
1.charCodeAt
格式:字符窜.charCodeAt(下标)
返回值:返回字符窜中对应下标的ASCII码
var str="hello";
alert(str.charCodeAt(1)); //101
2.String.fromCharCode()
格式:String.fromCharCode(ASCII码值---)
参数:ASCII码值个数任意
返回值:ASCII码值对应的字符组成的字符窜
str=String.fromCharCode(97,98,99);
alert(str); //abc
3.concat
格式:字符窜1.concat(字符窜2)
返回值:拼接成的字符窜