String 对象是用于处理文本(字符串)
string 是基础数据类型;
String 是对象;
let txt = new String(value);
//简单方式
let txt = "string";
属性 | 说明 |
---|---|
length | 返回字符串的长度 |
prototype | 允许向对象添加属性和方法 |
constructor | 创建该对象的函数的引用 |
方法 | 说明 |
---|---|
charAt | 返回在指定位置的字符 |
charCodeAt | 返回指定位置字符的 Unicode 编码 |
concat | 连接两个或更多字符串,并返回新的字符串 |
indexOf | 返回指定字符串值在字符串中首次出现位置 |
lastIndexOf | 搜索方向从后向前搜索最后出现字符串位置值,返回位置数值从前向后数 |
localeCompare | 用本地特定的顺序来比较两个字符串 |
match | 查找找到一个或多个正则表达式的匹配 |
replace | 替换与正则表达式匹配的子串 |
search | 检索与正则表达式相匹配的值 |
slice | 提取字符串的片断,并在新的字符串中返回被提取的部分 |
split | 把字符串分割为子字符串数组 |
substr | 返回从起始索引号开始指定长度的字符 |
substring | 提取字符串中指定索引号之间的字符 |
toLocaleLowerCase | 根据主机语言环境转换为小写 |
toLocaleUpperCase | 根据主机语言环境转换为大写 |
toLowerCase | 把字符串转换为小写 |
toString | 返回字符串 |
toUpperCase | 把字符串转换为大写 |
valueOf | 返回指定字符串对象的原始值 |
let str = new String( "This is string" );
console.log("str.constructor is",str.constructor)
//str.constructor is:function String() { [native code] }
console.log(str.constructor)
//ƒ String() { [native code] }
function employee(id:number,name:string) {
this.id = id
this.name = name
}
let emp = new employee(123,"admin")
employee.prototype.email = "admin@runoob.com"
console.log("员工邮箱: "+emp.email)
搜索方向从后向前,搜索字符串最后的位置值,返回位置值从前向后数
let str1 = new String( "This is string one and again string" );
let index = str1.lastIndexOf( "string" );
console.log("lastIndexOf 查找到的最后字符串位置 :" + index ); // 29
index = str1.lastIndexOf( "one" );
console.log("lastIndexOf 查找到的最后字符串位置 :" + index ); // 15
用本地特定的顺序来比较两个字符串
let str1 = new String( "This is beautiful string" );
let index = str1.localeCompare( "This is beautiful string");
// 0
console.log("localeCompare first :" + index );
替换与正则表达式匹配的子串
let re = /(\w+)\s(\w+)/;
let str = "zara ali";
let newstr = str.replace(re, "$2, $1");
console.log(newstr); // ali, zara
检索与正则表达式相匹配的值,未找到返回-1
let re1 = /apples/gi;
let re2 = /apples/g;
let re3 = /apples/;
let str = "Apples are round, apples are red, and apples are juicy.";
if (str.search(re) == -1 ) {
console.log("Does not contain Apples" );
} else {
console.log("Contains Apples", re1);//4
console.log("Contains Apples", re2);//22
console.log("Contains Apples", re3);//22
}
提取字符串的片断,并在新的字符串中返回被提取的部分
let re = 'apples';
let str = "Apples are round, apples are red, and apples are juicy.";
let newad=str.slice(re)
str="new word"
console.log("log1:" ,str)//"log1:new word"
console.log("log2:" ,newad)//"log2:Apples are round, apples are red, and apples are juicy."
返回的是新地址的字符串,因此修改该值不会影响原有字符串值
把字符串分割为子字符串数组。
string.split(splitChar, length)
let str = "Apples are round, and apples are juicy.";
let splitted = str.split(" ", 3);
console.log(splitted) // [ 'Apples', 'are', 'round,' ]
截取字符串中指定的两个索引号之间的字符
string.substring(start, end?)
let str = "RUNOOB GOOGLE TAOBAO FACEBOOK";
console.log("(1,2): " + str.substring(1,2)); // U
console.log("(0,10): " + str.substring(0, 10)); // RUNOOB GOO
console.log("(5): " + str.substring(5)); // B GOOGLE TAOBAO FACEBOOK
根据主机的语言环境把字符串转换为小写,只有几种语言(如土耳其语)具有地方特有的大小写映射。
let str = "Runoob Google";
console.log(str.toLocaleLowerCase( )); // runoob google
据主机的语言环境把字符串转换为大写,只有几种语言(如土耳其语)具有地方特有的大小写映射。
let str = "Runoob Google";
console.log(str.toLocaleUpperCase( )); // RUNOOB GOOGLE
返回指定字符串对象的原始值。
let str = new String("Runoob");
console.log(str.valueOf( )); // Runoob