var uname = new String("Hello World") console.log("Length "+uname.length) // 输出 11
prototype
允许您向对象添加属性和方法。
function employee(id:number,name:string) { this.id = id this.name = name } var emp = new employee(123,"admin") employee.prototype.email="admin@runoob.com" // 添加属性 email console.log("员工号: "+emp.id) console.log("员工姓名: "+emp.name) console.log("员工邮箱: "+emp.email)
2. 方法:
charAt()
返回在指定位置的字符。
var str = new String("RUNOOB"); console.log("str.charAt(0) 为:" + str.charAt(0)); // Rconsole.log("str.charAt(1) 为:" + str.charAt(1)); // U console.log("str.charAt(2) 为:" + str.charAt(2)); // N console.log("str.charAt(3) 为:" + str.charAt(3)); // O console.log("str.charAt(4) 为:" + str.charAt(4)); // O console.log("str.charAt(5) 为:" + str.charAt(5)); // B``` ^9e17b6
var str1 = new String( "This is string one and again string" ); var index = str1.lastIndexOf( "string" );console.log("lastIndexOf 查找到的最后字符串位置 :" + index ); // 29index = str1.lastIndexOf( "one" ); console.log("lastIndexOf 查找到的最后字符串位置 :" + index ); // 15
localeCompare()
用本地特定的顺序来比较两个字符串。
var str1 = new String( "This is beautiful string" );var index = str1.localeCompare( "This is beautiful string"); console.log("localeCompare first :" + index ); // 0