おそらくあなたが聞いたことがないJavaScriptストリングメソッドとそれは大丈夫です



概要


文字列、数字、記号、句読点、および絵文字を含めることができますテキストを表すためのJavaScriptのデータ型です.それらは、単独で囲まれた0個以上の' またはダブル" 引用符.
それはロケット科学ではない!前の導入について特別な何も、それは常にリフレッシュに良いです.信じられないかもしれないが、そこに開発者のトンは、すべての単一のJavaScriptの定義済みの方法についての手がかりを持って、私たちのほとんどは“私たちの頭に立ち往生している情報を使用して”大丈夫です.たとえば、私たちのほとんどは、我々が行くように文字列の最初の文字を取得することを知っているstr[0] これは全く大丈夫です.この記事では、5つのJavaScriptメソッドをカバーします.

文字列メソッドとプロパティー


文字列は原始的な値の1つであるので、例えば' mask off 'はプロパティやメソッドを持つことができません.幸いなことにJavaScriptは文字列を呼び出すいくつかのプロパティとメソッドを定義します.これらのプロパティとメソッドはドット表記を使用してアクセスされます.

Strings are immutable in JavaScript; and can be treated like read-only arrays. All string methods return a new value and they do not modify the string on which they were invoked.


例設定


Diggintoの詳細を見る前に、すべての例で使用できるブロックを最初に設定しましょう.
const content = "Forem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronics typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem IpsumL";

const { length }  = content;    // 500 characters
const firstChar   = 0;          // F
const lastChar    = length - 1; // L
const midContent  = length / 2; // e
const outOfRange  = length * 2; // ""

char ()メソッド


The charAt() このメソッドは、指定したインデックス位置に文字を返します.インデックスparamがない場合、デフォルトは0です.
/**
 * @param  {number} ranges from 0 to the length of the string -1
 * @return {string}
 */
string.charAt(index)

char ()の例


content.charAt() // "F"
content.charAt(firstChar)  // "F"
content.charAt(lastChar)   // "L"
content.charAt(midContent) // "e"
content.charAt(outOfRange) // ""

startswith()メソッド


The startsWith() メソッドは、文字列が指定した文字列から始まるかどうかを判断します.
/**
 * @param  {string} string to search for
 * @param  {number} optional - index, defaults to 0
 * @return {boolean}
 */
string.startsWith(string, position)

The startsWith() method is case sensitive.


startswith()の例


content.startsWith('F') // true
content.startsWith('f') // false
content.startsWith('e', midContent) // true

end ()メソッド


The endsWith() メソッドは、文字列が指定した文字列の文字で終了するかどうかを判断します.otherwise it returns false

endsgets ()の例


content.endsWith('L', lastChar) // true
content.endsWith('l', lastChar) // false

includes()メソッド


The includes() メソッドを使用すると、文字列が別の文字列を返すかどうかを確認できますBoolean
/**
 * @param  {string} string to search for
 * @param  {number} optional - index, defaults to 0
 * @return {boolean}
 */
string.includes(string, position)

includes()の例


content.includes('Ipsum') // true
content.includes('1960s') // true
content.includes('Hello from outside') // false

repeat ()メソッド


The repeat() メソッドは、呼び出された文字列の指定された数の文字列を一緒に連結し、新しい文字列を生成して返します.
/**
 * @param  {number} - indicating the number of times to repeat
 * @return {string}
 */
string.repeat(count)

repeat count must be: non-negative, less than infinity and not overflow maximum string size.


repeat ()の例


"**".repeat(3)  // "******"
"😺".repeat(3)  // "😺😺😺"
まとめる上記の方法は、別のアプローチで実装することができます、彼らはパフォーマンスで有害かもしれないか、彼らは最速の選択である可能性があります!途中で結果はあなたのニーズに依存します.
すべての利用可能なプロパティとメソッドをより詳細に見るには
私は非常に完全な読書をお勧めしますJavaScript String Reference .