TypeScript算法基础——TS字符串的常用操作总结:substring、indexOf、slice、replace等

本文介绍了在TypeScript中处理字符串时常用的一些API,包括获取长度、查找子串、替换内容、转换大小写、连接字符串以及将字符串转换为数组的方法,如length、indexOf、lastIndexOf、replace、toUpperCase、toLowerCase、concat、split等,对于编写算法题和日常开发非常实用。
摘要由CSDN通过智能技术生成

字符串的操作是算法题当中经常碰见的一类题目,主要考察对string类型的处理和运用。

在处理字符串的时候,我们经常会碰到求字符串长度、匹配子字符串、替换字符串内容、连接字符串、提取字符串字符等操作,那么调用一些简单好用的api可以让工作事半功倍,在TypeScript中,这些api其实和JavaScript的相同,下面整理一些比较常用的api:

1️⃣、返回字符串长度

length属性: 返回字符串的长度

	let txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	console.log(txt.length)  // 26

2️⃣、查找字符串

indexOf() 方法: 返回字符串中指定文本首次出现的索引(从0开始数,未找到则返回 -1):

	let txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	console.log(txt.indexOf('ABCD'))  // 0

lastIndexOf()方法: 返回字符串中指定文本最后一次出现的索引:

	let txt = "ABCDEFGHIJKLABCDEFGH";
	console.log(txt.lastIndexOf('ABCD'))  // 12

可以添加检索起始位置为第二个参数:txt.indexOf(“ABCD”, 5)、txt.lastindexOf(“ABCD”, 10):

    let txt = "ABCDEFGHIJKLABCDEFGH";
    console.log(txt.indexOf("ABCD", 5)); // 12
    console.log(txt.lastIndexOf("ABCD", 10)); // 0

includes() 方法: 如果字符串包含指定值,includes() 方法返回 true

    let txt = "a,b,c,aaa,bbb,aaa";
    console.log(txt.includes("aaa")); // true

3️⃣、提取字符串

slice()方法:提取字符串的某个部分并在新字符串中返回被提取的部分:

    let txt = "Apple, Banana, Mango";
    console.log(txt.slice(7)); //  Banana, Mango
    console.log(txt.slice(7,13)); //  Banana

如果某个参数为负,则从字符串的结尾开始计数:

    let txt = "Apple, Banana, Mango";
    console.log(txt.slice(7,-3)); // Banana, Ma

substring()方法: 类似于 slice(),提取字符串的某个部分并在新字符串中返回被提取的部分。

    let txt = "Apple, Banana, Mango";
    console.log(txt.substring(7));  //  Banana, Mango
    console.log(txt.substring(7,10)); // Ban

substr()方法:类似于 slice(),但第二个参数是被提取部分的长度。

    let txt = "Apple, Banana, Mango";
    console.log(txt.substr(7,4)); // Bana

4️⃣、替换字符串内容

replace() 方法: 用另一个值替换在字符串中指定的值,返回新字符串,默认只替换首个匹配:

let txt = "Apple, Banana, Mango";
console.log(txt.replace("Banana", "Orange")); // Apple, Orange, Mango
// 执行大小写不敏感的替换,使用正则表达式 /i 
console.log(txt.replace(/BAnana/i, "Orange")); // Apple, Orange, Mango
// 替换所有匹配,请使用正则表达式的 g 标志(用于全局搜索):
let txt = "Apple, Banana, Mango, Banana";
console.log(txt.replace(/Banana/g, "Orange")); // Apple, Orange, Mango, Orange

5️⃣、转换为大写和小写

toUpperCase() 方法:把字符串转换为大写:

    let txt = "Apple, Banana, Mango, Banana";
    console.log(txt.toUpperCase()); // APPLE, BANANA, MANGO, BANANA

toLowerCase() 方法:把字符串转换为大写:

    let txt = "Apple, Banana, Mango, Banana";
    console.log(txt.toLowerCase()); // apple, banana, mango, banana

6️⃣、连接字符串

concat() 方法:连接两个或多个字符串:

    let txt1 = "Apple";
    let txt2 = "Banana";
    let txt3 = "Mango";
    console.log(txt1.concat(" ", txt2, " ", txt3)); // Apple Banana Mango

7️⃣、提取字符串字符

charAt() 方法: 返回字符串中指定下标(位置)的字符串

    let txt1 = "Apple";
    console.log(txt1.charAt(1)); // p

charCodeAt() 方法: 返回字符串中指定索引的字符 unicode 编码

    let txt1 = "Apple";
    console.log(txt1.charCodeAt(1)); // 112

属性访问(不推荐):

    let txt1 = "Apple";
    console.log(txt1[1]); // p

8️⃣、切片把字符串转换为数组

split() 方法: 切片,以某个为分隔,将字符串转换为数组:

    let txt1 = "a,b,c";
    console.log(txt1.split("")); // [ 'a', ',', 'b', ',', 'c' ]
    console.log(txt1.split(",")); // [ 'a', 'b', 'c' ]

最后

💖 个人简介:2022年度博客之星总排名TOP12、人工智能领域TOP2,人工智能领域优质创作者。

📝 关注我:中杯可乐多加冰

🔥 限时免费订阅:TypeScript算法实战

📝 加入社群 抱团学习中杯可乐的答疑交流群

🎉 支持我:点赞👍+收藏⭐️+留言📝

  • 11
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
在Vue3中使用TypeScript进行字符串操作很简单。以下是一些常见的字符串操作的示例: 1. 字符串拼接: 可以使用模板字符串(template string)或者普通的字符串拼接操作符(+)来实现字符串的拼接。 ```typescript const str1: string = "Hello"; const str2: string = "World"; const result1: string = `${str1} ${str2}`; // 使用模板字符串 const result2: string = str1 + " " + str2; // 使用字符串拼接操作符 ``` 2. 字符串长度: 可以使用字符串的length属性来获取字符串的长度。 ```typescript const str: string = "Hello"; const length: number = str.length; console.log(length); // 输出: 5 ``` 3. 字符串截取: 可以使用slice()方法或者substring()方法来截取字符串的一部分。 ```typescript const str: string = "Hello World"; const substr1: string = str.slice(6, 11); // 使用slice()方法 const substr2: string = str.substring(6, 11); // 使用substring()方法 console.log(substr1); // 输出: World console.log(substr2); // 输出: World ``` 4. 字符串转换大小写: 可以使用toLowerCase()方法将字符串转换为小写,使用toUpperCase()方法将字符串转换为大写。 ```typescript const str: string = "Hello World";const lowerCase: string = str.toLowerCase(); // 转换为小写 const upperCase: string = str.toUpperCase(); // 转换为大写 console.log(lowerCase); // 输出: hello world console.log(upperCase); // 输出: HELLO WORLD ``` 5. 字符串查找: 可以使用indexOf()方法或者includes()方法来查找字符串中是否包含某个子字符串。 ```typescript const str: string = "Hello World"; const index: number = str.indexOf("World"); // 使用indexOf()方法 const includes: boolean = str.includes("World"); // 使用includes()方法 console.log(index); // 输出: 6 console.log(includes); // 输出: true ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

中杯可乐多加冰

请我喝杯可乐吧,我会多加冰!

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

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

打赏作者

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

抵扣说明:

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

余额充值