LoginSignup
1
0

More than 3 years have passed since last update.

【JavaScript】ある文字列のn番目の位置から、特定の文字あるいは単語が入っているかどうか調べたい

Posted at

はじめに

JavaScriptで、ある文字列のn番目の位置から、特定の文字あるいは単語が入っているかどうか確認したい。
そんな時、ありますよね。(競プロで使っただけです。)

そんな時に使えるのが、startsWithです。

startsWithとは

文字列のn番目からみて、指定した単語が含まれているかどうかを判定します。返り値はbooleanです。

// word.startsWith('調べたい文字列', 文字列の中の位置)

const word = '私はずんだもちが食べたい'

console.log(word.startsWith('私はずんだもち'))
// => true
console.log(word.startsWith('ずんだもち'))
// => false
console.log(word.startsWith('ずんだもち', 2))
// => true
console.log(word.startsWith('食べたい', 8))
// => true

これで、ある文字列のn番目の位置から、特定の文字あるいは単語が入っているかどうか確認できます!
やったね!

おしまい

こんなんいつ使うんだよ。
競プロは知らないメソッドとの出会いがあって楽しいですね。
自分のJavaScript力のなさを実感できます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0