TypeScript アルゴリズムの基礎 - TS 文字列の一般的な操作の概要: substring、indexOf、slice、replace。

TypeScript では、次のメソッドを使用して文字列に対する一般的な操作を実装できます。

よく使われる

  1. substring(startIndex: number, endIndex?: number): string: startIndex から endIndex までの部分文字列を返します (排他的)。endIndex を省略した場合は、startIndex から文字列の末尾までの部分文字列が返されます。
const str = "Hello, World!";
const subStr = str.substring(7, 12); // "World"
  1. indexOf(searchValue: string, startIndex?: number): number: 文字列内で最初に出現した searchValue のインデックス位置を返します。値が見つからない場合は、-1 が返されます。検索の開始位置は、startIndex パラメータを使用して指定できます。
const str = "Hello, World!";
const index = str.indexOf("World"); // 7
  1. slice(startIndex: number, endIndex?: number): string: startIndex から endIndex までの部分文字列を返します (排他的)。endIndex を省略した場合は、startIndex から文字列の末尾までの部分文字列が返されます。メソッドと似ていますsubstring()が、slice()メソッドは負のインデックスもサポートします。
const str = "Hello, World!";
const subStr = str.slice(7, 12); // "World"
  1. replace(searchValue: string | RegExp, replaceValue: string): string: 文字列内の searchValue を replaceValue に置き換え、新しい文字列を返します。searchValue には文字列または正規表現を指定できます。
const str = "Hello, World!";
const newStr = str.replace("World", "Universe"); // "Hello, Universe!"
  1. toUpperCase(): string: 文字列を大文字に変換します。
const str = "Hello, World!";
const upperCaseStr = str.toUpperCase(); // "HELLO, WORLD!"
  1. toLowerCase(): string: 文字列を小文字に変換します。
const str = "Hello, World!";
const lowerCaseStr = str.toLowerCase(); // "hello, world!"
  1. trim(): string: 文字列の両端のスペースを削除します。
const str = "   Hello, World!   ";
const trimmedStr = str.trim(); // "Hello, World!"

これらのメソッドは文字列処理で一般的に使用される操作であり、特定のニーズに応じて文字列を処理する適切なメソッドを選択できます。これらのメソッドはすべて新しい文字列を返し、元の文字列は変更されないことに注意してください。

文字列を検索

JavaScript/TypeScript では、文字列を検索するために使用できるさまざまな方法があります。一般的な方法をいくつか示します。

  1. indexOf(searchValue: string, startIndex?: number): number: 文字列内で最初に出現した searchValue のインデックス位置を返します。値が見つからない場合は、-1 が返されます。検索の開始位置は、startIndex パラメータを使用して指定できます。
const str = "Hello, World!";
const index = str.indexOf("World"); // 7
  1. lastIndexOf(searchValue: string, startIndex?: number): number: 文字列内で最後に出現した searchValue のインデックス位置を返します。値が見つからない場合は、-1 が返されます。検索の開始位置は、startIndex パラメータを使用して指定できます。
const str = "Hello, World!";
const index = str.lastIndexOf("o"); // 8
  1. search(regexp: string | RegExp): number: 正規表現を使用して文字列を検索し、最初に一致したインデックス位置を返します。一致するものが見つからない場合は -1 を返します。
const str = "Hello, World!";
const index = str.search(/World/); // 7
  1. includes(searchValue: string, startIndex?: number): boolean: 文字列に searchValue が含まれているかどうかを判断します。含まれている場合は true、含まれていない場合は false を返します。検索の開始位置は、startIndex パラメータを使用して指定できます。
const str = "Hello, World!";
const includes = str.includes("World"); // true
  1. startsWith(searchValue: string, startIndex?: number): boolean: 文字列が searchValue で始まるかどうかを判断します。はいの場合は true を返し、そうでない場合は false を返します。検索の開始位置は、startIndex パラメータを使用して指定できます。
const str = "Hello, World!";
const startsWith = str.startsWith("Hello"); // true
  1. endsWith(searchValue: string, endIndex?: number): boolean: 文字列が searchValue で終わるかどうかを判断します。はいの場合は true を返し、そうでない場合は false を返します。検索の終了位置は、endIndex パラメータを使用して指定できます。
const str = "Hello, World!";
const endsWith = str.endsWith("World"); // false

上記は一般的に使用される文字列検索方法の一部であり、特定のニーズに応じて適切な方法を選択して文字列を検索します。これらのメソッドはすべて、特定の一致する文字列ではなく、ブール値またはインデックス位置を返すことに注意してください。

文字列を抽出する

JavaScript/TypeScript には、文字列の部分文字列を抽出するために使用できるさまざまなメソッドがあります。一般的な方法をいくつか示します。

  1. substring(startIndex: number, endIndex?: number): string: startIndex から endIndex までの部分文字列を返します (排他的)。endIndex を省略した場合は、startIndex から文字列の末尾までの部分文字列が返されます。メソッドと似ていますslice()が、substring()メソッドは負のインデックスをサポートしません。
const str = "Hello, World!";
const subStr = str.substring(7, 12); // "World"
  1. substr(startIndex: number, length?: number): string: startIndex から始まる length の長さの部分文字列を返します。length を省略した場合、startIndex から文字列の末尾までの部分文字列が返されます。
const str = "Hello, World!";
const subStr = str.substr(7, 5); // "World"
  1. slice(startIndex: number, endIndex?: number): string: startIndex から endIndex までの部分文字列を返します (排他的)。endIndex を省略した場合は、startIndex から文字列の末尾までの部分文字列が返されます。メソッドと似ていますsubstring()が、slice()メソッドは負のインデックスもサポートします。
const str = "Hello, World!";
const subStr = str.slice(7, 12); // "World"
  1. split(separator: string | RegExp, limit?: number): string[]: 文字列を部分文字列の配列に分割し、指定された区切り文字に従って分割します。返される部分文字列の数は、limit パラメーターを使用して制限できます。
const str = "Hello, World!";
const parts = str.split(","); // ["Hello", " World!"]

これらのメソッドは、特定の要件に従って文字列の部分文字列を抽出するための適切な方法を選択できます。これらのメソッドは新しい文字列または文字列の配列を返し、元の文字列は変更されないことに注意してください。

おすすめ

転載: blog.csdn.net/ACCPluzhiqi/article/details/132068528