In TypeScript, common operations of strings can be implemented using the following methods:
Commonly used
substring(startIndex: number, endIndex?: number): string: Returns a substring from startIndex to endIndex (excluding). If endIndex is omitted, a substring from startIndex to the end of the string is returned.
const str = "Hello, World!"; const subStr = (7, 12); // "World"
indexOf(searchValue: string, startIndex?: number): return the index position where the searchValue first appears in the string. If the value is not found, return -1. You can use the startIndex parameter to specify the starting position of the search.
const str = "Hello, World!"; const index = ("World"); // 7
slice(startIndex: number, endIndex?: number): string: Returns a substring from startIndex to endIndex (excluding). If endIndex is omitted, a substring from startIndex to the end of the string is returned. Similar to the substring() method, but the slice() method also supports negative indexing.
const str = "Hello, World!"; const subStr = (7, 12); // "World"
replace(searchValue: string | RegExp, replaceValue: string): string: Replace the searchValue in the string with replaceValue and return the new string. The searchValue can be a string or a regular expression.
const str = "Hello, World!"; const newStr = ("World", "Universe"); // "Hello, Universe!"
toUpperCase(): string: converts a string to uppercase.
const str = "Hello, World!"; const upperCaseStr = (); // "HELLO, WORLD!"
toLowerCase(): string: converts a string to lowercase.
const str = "Hello, World!"; const lowerCaseStr = (); // "hello, world!"
trim(): string: Remove spaces at both ends of the string.
const str = " Hello, World! "; const trimmedStr = (); // "Hello, World!"
These methods are commonly used operations in string processing, and you can choose appropriate methods to process strings according to specific needs. It should be noted that these methods return new strings, and the original string will not be modified.
Find strings
In JavaScript/TypeScript, there are several ways to find strings. Here are a few common methods:
indexOf(searchValue: string, startIndex?: number): return the index position where the searchValue first appears in the string. If the value is not found, return -1. You can use the startIndex parameter to specify the starting position of the search.
const str = "Hello, World!"; const index = ("World"); // 7
lastIndexOf(searchValue: string, startIndex?: number): return the index position of the last occurrence of searchValue in the string. If the value is not found, return -1. You can use the startIndex parameter to specify the starting position of the search.
const str = "Hello, World!"; const index = ("o"); // 8
search(regexp: string | RegExp): number: Use regular expression to search for the string and return the first matching index position. If no match is found, return -1.
const str = "Hello, World!"; const index = (/World/); // 7
include(searchValue: string, startIndex?: number): boolean: determines whether searchValue is included in the string. Return true if included, otherwise return false. You can use the startIndex parameter to specify the starting position of the search.
const str = "Hello, World!"; const includes = ("World"); // true
startsWith(searchValue: string, startIndex?: number): boolean: determines whether the string starts with searchValue. If yes, return true, otherwise false. You can use the startIndex parameter to specify the starting position of the search.
const str = "Hello, World!"; const startsWith = ("Hello"); // true
endsWith(searchValue: string, endIndex?: number): boolean: determines whether the string ends with searchValue. If yes, return true, otherwise false. You can use the endIndex parameter to specify the end position of the search.
const str = "Hello, World!"; const endsWith = ("World"); // false
The above are some commonly used string search methods, and select appropriate methods to find strings according to specific needs. It should be noted that these methods all return boolean values or index positions, rather than specific matching strings.
Extract strings
In JavaScript/TypeScript, there are several ways to extract substrings of strings. Here are a few common methods:
substring(startIndex: number, endIndex?: number): string: Returns a substring from startIndex to endIndex (excluding). If endIndex is omitted, a substring from startIndex to the end of the string is returned. Similar to the slice() method, but the substring() method does not support negative indexing.
const str = "Hello, World!"; const subStr = (7, 12); // "World"
substr(startIndex: number, length?: number): string: Returns a substring starting from startIndex and length is length. If length is omitted, a substring from startIndex to the end of the string is returned.
const str = "Hello, World!"; const subStr = (7, 5); // "World"
slice(startIndex: number, endIndex?: number): string: Returns a substring from startIndex to endIndex (excluding). If endIndex is omitted, a substring from startIndex to the end of the string is returned. Similar to the substring() method, but the slice() method also supports negative indexing.
const str = "Hello, World!"; const subStr = (7, 12); // "World"
split(separator: string | RegExp, limit?: number): string[]: splits the string into a substring array and divides it according to the specified separator. The limit parameter can be used to limit the number of substrings returned.
const str = "Hello, World!"; const parts = (","); // ["Hello", " World!"]
These methods can select appropriate methods according to specific needs to extract substrings of strings. It should be noted that these methods return a new string or string array, and the original string will not be modified.
This is the end of this article about the summary of common operations of TypeScript strings. For more related TypeScript string content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!