字符串使用来存储和处理文本的。String的下标从0开始
1.定义
方法1: var zifu="zifu";var zifu=‘zifu’;var zifu=new String("zifu"); 第三种不建议拖慢页面。
2.属性
1.length 获取长度 2.constructor 返回字符串属性函数 3. prototype 允许您向对象添加属性和方法
比如:
var zifuchuang = ‘zifuchuang’; console.log(zifuchuang.length); console.log(zifuchuang.constructor);
prototype比较特殊
举例: var deme="ceshide";
String.prototype.name = "cedde";//为String添加了 name的属性
demo.name="ceshide"; document.write(demo.name);
3.方法。针对String对象方法分为三类。
1.针对字符的: indexOf();LastIndexOf();replace();split();charAt();charCodeAt();slice()提前片段;substr();
2.针对字符串的: toLocalUpperCase();toLocalLowerCase();trim();
3.检验,验证的:search();match()
举例:var str="The rain in SPAIN stays mainly in the plain"; var n=str.match(/ain/g);document.write(n);
var str="Mr. Blue has a blue house";document.write(str.search("blue")); 返回查到blue的字符位置。