TypeScript字符串常用操作:提取、查找与转换详解
本文系统总结TypeScript中字符串的常用操作方法,涵盖提取、查找与格式转换三大类。详细解析substring、slice、indexOf、includes等核心API的使用场景、参数差异及返回值类型,并提供实际代码示例,帮助开发者高效处理字符串数据。
在TypeScript开发中,字符串处理是基础且高频的操作。掌握以下常用方法,可以高效实现字符串的提取、查找与格式转换等功能。
常用
substring(startIndex: number, endIndex?: number): string:该方法用于截取子字符串,返回从startIndex开始至endIndex(不含)之间的内容。若未提供endIndex参数,则截取从startIndex到字符串末尾的部分。
const str = "Hello, World!";
const subStr = str.substring(7, 12); // "World"indexOf(searchValue: string, startIndex?: number): number:用于查找指定字符串searchValue首次出现的位置,返回其索引值。若未找到匹配项,返回-1。可通过startIndex参数指定搜索起始点。
const str = "Hello, World!";
const index = str.indexOf("World"); // 7slice(startIndex: number, endIndex?: number): string:功能与substring类似,返回从startIndex到endIndex(不含)的子字符串。若省略endIndex,则截取至末尾。与substring不同,slice支持负数索引,表示从字符串末尾倒数。
const str = "Hello, World!";
const subStr = str.slice(7, 12); // "World"replace(searchValue: string | RegExp, replaceValue: string): string:用于替换字符串中的指定内容。searchValue可以是普通字符串或正则表达式,replaceValue为替换后的新字符串。该方法返回修改后的新字符串。
const str = "Hello, World!";
const newStr = str.replace("World", "Universe"); // "Hello, Universe!"toUpperCase(): string:将字符串中的所有字符转换为大写形式,并返回新字符串。
const str = "Hello, World!";
const upperCaseStr = str.toUpperCase(); // "HELLO, WORLD!"toLowerCase(): string:将字符串中的所有字符转换为小写形式,并返回新字符串。
const str = "Hello, World!";
const lowerCaseStr = str.toLowerCase(); // "hello, world!"trim(): string:移除字符串首尾的空格字符,返回去除空格后的新字符串。
const str = " Hello, World! ";
const trimmedStr = str.trim(); // "Hello, World!"上述方法是字符串处理中最基础且常用的操作。开发者可根据具体需求选择合适的方法。需注意的是,这些方法均不会修改原始字符串,而是返回一个新的字符串对象。
查找字符串
在JavaScript和TypeScript中,查找字符串内容有多种内置方法可用。以下介绍几种常见的字符串查找API及其用法。
indexOf(searchValue: string, startIndex?: number): number:返回目标字符串searchValue在源字符串中首次出现的索引位置。若未找到,返回-1。支持通过startIndex参数指定搜索起始位置。
const str = "Hello, World!";
const index = str.indexOf("World"); // 7lastIndexOf(searchValue: string, startIndex?: number): number:返回目标字符串searchValue在源字符串中最后一次出现的索引位置。若未找到,返回-1。同样支持通过startIndex参数指定搜索起始位置。
const str = "Hello, World!";
const index = str.lastIndexOf("o"); // 8search(regexp: string | RegExp): number:使用正则表达式regexp对字符串进行搜索,返回第一个匹配项的索引位置。若未找到匹配内容,返回-1。
const str = "Hello, World!";
const index = str.search(/World/); // 7includes(searchValue: string, startIndex?: number): boolean:判断字符串中是否包含指定的searchValue。若包含,返回true;否则返回false。支持通过startIndex参数指定搜索起始位置。
const str = "Hello, World!";
const includes = str.includes("World"); // truestartsWith(searchValue: string, startIndex?: number): boolean:判断字符串是否以指定的searchValue开头。若是,返回true;否则返回false。支持通过startIndex参数指定从何处开始判断。
const str = "Hello, World!";
const startsWith = str.startsWith("Hello"); // trueendsWith(searchValue: string, endIndex?: number): boolean:判断字符串是否以指定的searchValue结尾。若是,返回true;否则返回false。支持通过endIndex参数指定判断的结束位置。
const str = "Hello, World!";
const endsWith = str.endsWith("World"); // false以上方法涵盖了字符串查找的主要需求。根据具体场景,可选择返回布尔值或索引值的方法。需注意,这些方法返回的是判断结果或位置索引,而非匹配到的具体字符串内容。
提取字符串
在JavaScript和TypeScript中,提取字符串子串有多种内置方法。以下介绍几种常用的字符串提取API及其用法。
substring(startIndex: number, endIndex?: number): string:返回从startIndex开始至endIndex(不含)之间的子字符串。若省略endIndex,则截取至字符串末尾。与slice方法类似,但substring不支持负数索引。
const str = "Hello, World!";
const subStr = str.substring(7, 12); // "World"substr(startIndex: number, length?: number): string:返回从startIndex开始,长度为length的子字符串。若省略length参数,则截取从startIndex到字符串末尾的部分。
const str = "Hello, World!";
const subStr = str.substr(7, 5); // "World"slice(startIndex: number, endIndex?: number): string:返回从startIndex开始至endIndex(不含)之间的子字符串。若省略endIndex,则截取至字符串末尾。与substring方法类似,但slice支持负数索引,表示从字符串末尾倒数。
const str = "Hello, World!";
const subStr = str.slice(7, 12); // "World"split(separator: string | RegExp, limit?: number): string[]:根据指定的分隔符separator将字符串分割为子字符串数组。可通过limit参数限制返回数组的元素数量。
const str = "Hello, World!";
const parts = str.split(","); // ["Hello", " World!"]上述方法可根据具体需求用于提取字符串子串。需注意,这些方法均返回新的字符串或字符串数组,原始字符串保持不变。
本文介绍了TypeScript中字符串的常用操作方法,包括提取、查找与格式转换等。更多相关内容可参考本站其他文章或继续浏览后续内容,感谢支持。
JS split切割后出现空字符串怎么过滤
2026-11-05 16:00:30
在 React 或 Vue 项目中封装使用 JS 节流防抖
2026-11-05 15:27:29
JavaScript中怎么将带小数的字符串转为整数?
2026-11-05 14:32:38
怎么用JavaScript策略模式重构电商促销计算逻辑?
2026-11-05 14:01:09
JS使用constructor判断数据类型可靠吗
2026-11-05 13:49:41








