String 对象
(1) 创建string对象
String 对象用于操纵和处理文本串,可以通过的该对象在程序中获取字符串的长度,提取子字符串,以及字符串转换为大写或小写字符。
语法 :
var newstr = new String(text) ;
例如:
var newstr = new String(" 欢迎使用JavaScript 脚本");
事实上任何一个字符串常量(使用引号或双引号括起来的字符串)都是一个String对象,可以将其直接作为对象来使用,只要在字符变量的后面加“.”,便可以直接调用String对象的属性和方法,字符串与String对象的不同在于返回typeof 值,前者返回的是String 类型,后者返回的是Object类型。
(2)String 对象的属性
①length
object.length
例子:
var newString = new String("欢迎使用JavaScript脚本");
var p = newString.length;
document.write(p.toString(16));
②constructor 属性
该属性用于对当前对象的函数的引用
object.constructor
例子:
var newString =new String("sdl");
if(newString.constructor == String){
alert("this is string ");
③prototype 属性
该属性可以为对象添加属性和方法
object.prototype.name = value;
name 是object 对象要添加的属性
例子:
function person(name ,year) {
this.name = name ;
this.year = year;
}
var information = new person("wsj",24);
person.prototype.height = null;
information.height = 170;
alert(information.height);
③String 对象的方法
var object = new String("吴胜杰");
var object1 = new String("wsj");
// document.write( object.anchor("word"));//是创建HTML锚
// document.write(object.big()); //使用大号字体
// document.write(object.small());// 使用小号字体
// document.write(object.fontsize(10));//使用指定尺寸的字号
// document.write(object.bold());//使用粗体显示字符串
// document.write(object.italics());//使用斜体显示字符串
// document.write(object.link());//使用链接的方式显示字符串
// document.write(object.strike());//使用删除线的方式显示字符串
// document.write(object.blink());//使用闪动的方式显示字符串
// document.write(object.fixed());//使用打印机的字体显示字符串
//document.write(object.charAt(0));//返回指定位置的字符
//document.write(object.charCodeAt(0));//返回指定位置的字符(以编码的方式),
/* object.concat(object1);//连接字符 吴胜杰wsj
document.write(object.concat(object1));*/
//document.write(object.fontcolor("red"));//指定显示字符串的颜色
//document.write(object1.fromCharCode());
// document.write(object.indexOf("胜"));//该方法返回该字 在字符串的位置
// document.write(object.indexOf("胜",0));//该方法是从第几个位置开始索引该字符的位置
// document.write(object.lastIndexOf("杰"));//该方法是从最后一个位子开始索引 该字符的位置
上面只是列了一些方法,没有全部列出,还有字符转换大小写以及字符串的索引,如有需要敬请百度详细查询。