【徹底比較】JavaScriptで部分文字列の存在を確認する3つの方法のメリットとデメリット
JavaScriptで部分文字列の存在を確認する方法
String.prototype.includes() メソッド
概要
includes()
メソッドは、指定された部分文字列が文字列内に含まれているかどうかを調べ、真偽値を返します。最もシンプルで分かりやすい方法です。
例
const str = "Hello, world!";
console.log(str.includes("world")); // true
console.log(str.includes("JavaScript")); // false
メリット
- シンプルで分かりやすい
- 大文字小文字を区別する/しないをオプションで指定できる
- 正規表現のような高度な検索には対応していない
indexOf()
メソッドは、指定された部分文字列が文字列内に最初に現れる位置を返します。部分文字列が存在しない場合は -1 を返します。
const str = "Hello, world!";
const index = str.indexOf("world");
if (index !== -1) {
console.log("部分文字列 'world' は存在します");
} else {
console.log("部分文字列 'world' は存在しません");
}
- 部分文字列の存在だけでなく、位置も取得できる
- 大文字小文字を区別しない場合は、事前に変換する必要がある
正規表現
正規表現は、より複雑なパターンマッチングを行うことができます。大文字小文字の区別、繰り返しパターン、前方一致・後方一致など、さまざまな条件を設定できます。
const str = "Hello, world!";
const regex = /world/i; // 'i' オプションで大文字小文字を区別しない
const result = regex.test(str);
if (result) {
console.log("部分文字列 'world' は存在します");
} else {
console.log("部分文字列 'world' は存在しません");
}
- 複雑なパターンマッチングが可能
- 記述が複雑になる
- 正規表現の知識が必要
- シンプルな方法で部分文字列の存在だけを確認したい場合は、
includes()
メソッドがおすすめです。 - 部分文字列の位置も取得したい場合は、
indexOf()
メソッドを使います。 - 複雑なパターンマッチングを行う場合は、正規表現を使います。
それぞれの方法のメリットとデメリットを理解して、適切な方法を選びましょう。
- 上記以外にも、
String.prototype.startsWith()
やString.prototype.endsWith()
などのメソッドも部分文字列の存在を確認するのに使えます。 - 大文字小文字を区別しない場合は、
toLowerCase()
やtoUpperCase()
メソッドを使って事前に変換しておくと、比較が楽になります。
String.prototype.includes() メソッド
const str = "Hello, world!";
console.log(str.includes("world")); // true
console.log(str.includes("JavaScript")); // false
String.prototype.indexOf() メソッド
const str = "Hello, world!";
const index = str.indexOf("world");
if (index !== -1) {
console.log("部分文字列 'world' は存在します");
} else {
console.log("部分文字列 'world' は存在しません");
}
正規表現
const str = "Hello, world!";
const regex = /world/i; // 'i' オプションで大文字小文字を区別しない
const result = regex.test(str);
if (result) {
console.log("部分文字列 'world' は存在します");
} else {
console.log("部分文字列 'world' は存在しません");
}
部分文字列の存在を確認する他の方法
forループ
forループを使って、文字列を1文字ずつ比較していく方法です。
const str = "Hello, world!";
const subStr = "world";
let found = false;
for (let i = 0; i < str.length; i++) {
if (str[i] === subStr[0]) {
for (let j = 0; j < subStr.length; j++) {
if (str[i + j] !== subStr[j]) {
break;
}
}
if (j === subStr.length) {
found = true;
break;
}
}
}
if (found) {
console.log("部分文字列 'world' は存在します");
} else {
console.log("部分文字列 'world' は存在しません");
}
- 他の方法に比べて処理速度が速い場合がある
String.prototype.split() メソッド
split()
メソッドを使って、部分文字列で文字列を分割し、分割された配列の長さを確認する方法です。
const str = "Hello, world!";
const subStr = "world";
const arr = str.split(subStr);
if (arr.length > 1) {
console.log("部分文字列 'world' は存在します");
} else {
console.log("部分文字列 'world' は存在しません");
}
- 記述が比較的簡単
- 分割された配列を必要としない場合は、処理効率が良くない
ライブラリ
Lodashなどのライブラリには、部分文字列の存在を確認する関数などが用意されています。
const _ = require("lodash");
const str = "Hello, world!";
const subStr = "world";
const found = _.includes(str, subStr);
if (found) {
console.log("部分文字列 'world' は存在します");
} else {
console.log("部分文字列 'world' は存在しません");
}
- さまざまな機能が利用できる
- ライブラリの読み込みが必要
部分文字列の存在を確認する方法はいくつかあります。それぞれの特徴を理解して、状況に合わせて適切な方法を選びましょう。
javascript string substring