JavaScript 文字列メソッド

5201 ワード

今日は、最も重要な JavaScript 文字列メソッドについて、説明と例を交えて説明します.

charAt(): このメソッドは、文字列の文字を抽出するために使用されます.このメソッドは、文字列内の特定のインデックスにある文字を返します.

let text = "FEROJ ALAM";
let char = text.charAt(0); //output F


concat(): このメソッドは、2 つ以上の文字列を結合するために使用されます.

let text1 = "Feroj";
let text2 = "Alam";
let text3 = text1.concat(" ", text2); //output Hello World


プラス演算子の代わりに concat() を使用できます.

text = "Feroj" + " " + "Alam";  //output Feroj Alam
text = "Hello".concat(" ", "World!");  //output Hello World


これらの 2 つの行は同じです

includes(): このメソッドは、検索している文字列がその文字列に存在することを確認するために使用されます.文字列が存在する場合は true を返し、存在しない場合は false を返します.

let text = "Feroj Alam, web developer.";
let result = text.includes("web"); // true


includes() メソッドは大文字と小文字を区別します.

endWith(): このメソッドは、文字列が検索対象の文字列で終わっていることを確認するために使用されます.存在する場合は true を返し、存在しない場合は false を返します.このメソッドは大文字と小文字を区別します.

let text = "Programming Hero";
let result = text.endsWith("Hero");//output true
let text = "Programming Hero";
let result = text.endsWith("hero");//output false


indexOf(): このメソッドは、文字列の位置を見つけるために使用されます.このメソッドは、探している最初に見つかった文字列を返します.見つからない場合は -1 を返します.このメソッドも大文字と小文字を区別します.

let text = "Hello world, welcome to the universe.";
let result = text.indexOf("welcome"); // output 13
let text = "Hello world, welcome to the universe.";
let result = text.indexOf("Welcome"); // output -1


lastIndexOf(): このメソッドは、最後に配置された文字列の位置を見つけるために使用されます.最後から文字列を探し始めます. 0 から始まるインデックスを与えます.値が見つからない場合は -1 を返します.このメソッドは大文字と小文字を区別します.

let text = "Hello planet earth, you are a great planet.";
let result = text.lastIndexOf("planet"); //output 36
let text = "Hello planet earth, you are a great planet.";
let result = text.lastIndexOf("Planet"); //output -1


replace(): このメソッドは、指定された値または正規表現の文字列を置換するために使用されます.値が置き換えられた新しい文字列を返します.元の文字列は変更されません.

let text = "Visit Microsoft!";
let result = text.replace("Microsoft", "Google"); //output Visit Google
let text = "Mr Blue has a blue house and a blue car";
let result = text.replace(/blue/g, "red"); //output Mr Blue has a red house and a red car


slice(): このメソッドは、特定の文字列を抽出するために使用されます.抽出された文字列を新しい文字列で返しますが、元の文字列は変更しません. (0, 5) のようにスライスまたはカットに開始インデックスと終了インデックスを指定する必要があります.負の値を指定すると、文字列の最後から抽出されます.

let text = "Hello world!";
let result = text.slice(0, 5);//output Hello
let result = text.slice(3);//output lo world!


split(): このメソッドは、単語を分割または分離して配列を作成するために使用されます.新しい配列を返します.元の文字列は変更されません.

let text = "How are you doing today?";
const myArray = text.split(" "); //output How,are,you,doing,today?
const myArray = text.split(""); //output H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?


("") これをセパレータとして使用すると、文字列が単語に分割されます

startsWith(): このメソッドは、文字列が検索対象の文字列で始まることを確認するために使用されます.見つかった場合は true を返し、それ以外の場合は false を返します.このメソッドは大文字と小文字を区別します.

let text = "Hello world, welcome to the universe.";
text.startsWith("Hello"); //output true


substr(): 文字列の一部を抽出またはカットするために使用されます.抽出の開始位置と終了位置を受け取ります.元の文字列は変更しません.

let text = "Hello world!";
let result = text.substr(1, 4); //output ello
let result = text.substr(2); //output llo world!


toLowerCase(): このメソッドは、文字列の文字を小文字に変換するために使用されます.元の文字列は変更されません.

let text = "Hello World!";
let result = text.toLowerCase(); //output hello world!


toUpperCase(): 文字列の文字を大文字に変換するために使用されます.また、元の文字列は変更されません.

let text = "Hello World!";
let result = text.toUpperCase(); //HELLO WORLD!


trim(): このメソッドは、文字列の両側から余分なスペースを削除するために使用されます.元の文字列は変更されません.

let text = "    Hello World!        ";
let result = text.trim();//output Hello World!


trimStart(): このメソッドは、文字列の先頭から空白を削除するために使用されます.空白がない場合は、エラーをスローせずに新しい文字列を返します.

let text = "        Hello World!";
let result = text.trimStart();//output Hello World!


trimEnd(): 文字列の末尾から空白を削除するために使用されます.空白がない場合、エラーをスローせずに新しい文字列を返します.

let text = "Hello World!          ";
let result = text.trimEnd();//output Hello World!