教程 > typescript 教程 > 阅读:118

typescript string(字符串)——迹忆客-ag捕鱼王app官网

string 对象用于处理文本(字符串)。

语法

var txt = new string("string");
// 或者更简单方式:
var txt = "string";

string 对象属性

下表列出了 string 对象支持的属性:

序号 属性 & 描述 实例
1 constructor 对创建该对象的函数的引用。
var str = new string( "this is string" ); 
console.log("str.constructor is:" str.constructor)
输出结果:
str.constructor is:function string() { [native code] }
2 length 返回字符串的长度。
var uname = new string("hello world") 
console.log("length " uname.length) // 输出 11
3 prototype 允许您向对象添加属性和方法。
function employee(id:number,name:string) { 
this.id = id
this.name = name
}
var emp = new employee(123,"admin")
employee.prototype.email="" // 添加属性 email
console.log("员工号: " emp.id)
console.log("员工姓名: " emp.name)
console.log("员工邮箱: " emp.email)

string 方法

下表列出了 string 对象支持的方法:

序号 方法 & 描述 实例
1 charat() 返回在指定位置的字符。
var str = new string("jiyik"); 
console.log("str.charat(0) 为:" str.charat(0)); // j
console.log("str.charat(1) 为:" str.charat(1)); // i
console.log("str.charat(2) 为:" str.charat(2)); // y
console.log("str.charat(3) 为:" str.charat(3)); // i
console.log("str.charat(4) 为:" str.charat(4)); // k
2 charcodeat() 返回在指定的位置的字符的 unicode 编码。
var str = new string("jiyik"); 
console.log("str.charcodeat(0) 为:" str.charcodeat(0)); // 74
console.log("str.charcodeat(1) 为:" str.charcodeat(1)); // 73
console.log("str.charcodeat(2) 为:" str.charcodeat(2)); // 89
console.log("str.charcodeat(3) 为:" str.charcodeat(3)); // 73
console.log("str.charcodeat(4) 为:" str.charcodeat(4)); // 75
console.log("str.charcodeat(5) 为:" str.charcodeat(5)); // nan
3 concat() 连接两个或更多字符串,并返回新的字符串。
var str1 = new string( "jiyik" ); 
var str2 = new string( "google" );
var str3 = str1.concat( str2 );
console.log("str1 str2 : " str3) // jiyikgoogle
4 indexof() 返回某个指定的字符串值在字符串中首次出现的位置。
var str1 = new string( "jiyik" ); 
var index = str1.indexof( "y" );
console.log("查找的字符串位置 :" index );
5 lastindexof() 从后向前搜索字符串,并从起始位置(0)开始计算返回字符串最后出现的位置。
var str1 = new string( "this is string one and again string" ); 
var index = str1.lastindexof( "string" );
console.log("lastindexof 查找到的最后字符串位置 :" index ); // 29

index = str1.lastindexof( "one" );
console.log("lastindexof 查找到的最后字符串位置 :" index ); // 15
6 localecompare() 用本地特定的顺序来比较两个字符串。
var str1 = new string( "this is beautiful string" );

var index = str1.localecompare( "this is beautiful string");
console.log("localecompare first :" index ); // 0
7 match() 查找找到一个或多个正则表达式的匹配。
var str="the rain in spain stays mainly in the plain"; 
var n=str.match(/ain/g); // ain,ain,ain
8 replace() 替换与正则表达式匹配的子串
var re = /(\w )\s(\w )/; 
var str = "zara ali";
var newstr = str.replace(re, "$2, $1");
console.log(newstr); // ali, zara
9 search() 检索与正则表达式相匹配的值
var re = /apples/gi; 
var str = "apples are round, and apples are juicy.";
if (str.search(re) == -1 ) {
console.log("does not contain apples" );
} else {
console.log("contains apples" );
}
10 slice() 提取字符串的片断,并在新的字符串中返回被提取的部分。
11 split() 把字符串分割为子字符串数组。
var str = "apples are round, and apples are juicy."; 
var splitted = str.split(" ", 3);
console.log(splitted) // [ 'apples', 'are', 'round,' ]
12 substr() 从起始索引号提取字符串中指定数目的字符。
13 substring() 提取字符串中两个指定的索引号之间的字符。
var str = "jiyik google taobao facebook"; 
console.log("(1,2): " str.substring(1,2)); // i
console.log("(0,10): " str.substring(0, 10)); // jiyik goog
console.log("(5): " str.substring(5)); // google taobao facebook
14 tolocalelowercase() 根据主机的语言环境把字符串转换为小写,只有几种语言(如土耳其语)具有地方特有的大小写映射。
var str = "jiyik google"; 
console.log(str.tolocalelowercase( )); // jiyik google
15 tolocaleuppercase() 据主机的语言环境把字符串转换为大写,只有几种语言(如土耳其语)具有地方特有的大小写映射。
var str = "jiyik google"; 
console.log(str.tolocaleuppercase( )); // jiyik google
16 tolowercase() 把字符串转换为小写。
var str = "jiyik google"; 
console.log(str.tolowercase( )); // jiyik google
17 tostring() 返回字符串。
var str = "jiyik"; 
console.log(str.tostring( )); // jiyik
18 touppercase() 把字符串转换为大写。
var str = "jiyik google"; 
console.log(str.touppercase( )); // jiyik google
19 valueof() 返回指定字符串对象的原始值。
var str = new string("jiyik"); 
console.log(str.valueof( )); // jiyik

查看笔记

扫码一下
查看教程更方便
网站地图