[Typescript]基础篇之 String 对象

String 对象简介

String 对象是用于处理文本(字符串)

与 string 区别

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返回指定字符串对象的原始值

属性的使用

  1. constructor
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] }
  1. prototype
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)

方法的使用

lastIndexOf

搜索方向从后向前,搜索字符串最后的位置值,返回位置值从前向后数

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

localeCompare

用本地特定的顺序来比较两个字符串

let str1 = new String( "This is beautiful string" );

let index = str1.localeCompare( "This is beautiful string");
// 0
console.log("localeCompare first :" + index );

replace

替换与正则表达式匹配的子串

let re = /(\w+)\s(\w+)/;
let str = "zara ali";
let newstr = str.replace(re, "$2, $1");
console.log(newstr); // ali, zara

search

检索与正则表达式相匹配的值,未找到返回-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
}
  • /gi 搜索第一次检索到正则的位置值,不区分大小写
  • /g 搜索第一次检索到正则的位置值,区分大小写
  • / 搜索第一次检索到正则的位置值,区分大小写

slice

提取字符串的片断,并在新的字符串中返回被提取的部分

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."

返回的是新地址的字符串,因此修改该值不会影响原有字符串值

split

把字符串分割为子字符串数组。

string.split(splitChar, length)
  • splitChar 用于分割的字符串
  • string 被分割字符串
  • length 分割字符串后返回的字符串数组的长度
let str = "Apples are round, and apples are juicy.";
let splitted = str.split(" ", 3);
console.log(splitted)  // [ 'Apples', 'are', 'round,' ]

substring

截取字符串中指定的两个索引号之间的字符

string.substring(start, end?)
  • start 截取字符串开始位置索引,若是没有 end,默认为截取到最后一个字符串
  • 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

toLocaleLowerCase

根据主机的语言环境把字符串转换为小写,只有几种语言(如土耳其语)具有地方特有的大小写映射。

let str = "Runoob Google";
console.log(str.toLocaleLowerCase( ));  // runoob google

toLocaleUpperCase()

据主机的语言环境把字符串转换为大写,只有几种语言(如土耳其语)具有地方特有的大小写映射。

let str = "Runoob Google";
console.log(str.toLocaleUpperCase( ));  // RUNOOB GOOGLE

valueOf()

返回指定字符串对象的原始值。

let str = new String("Runoob");
console.log(str.valueOf( ));  // Runoob
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三知之灵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值