Last active
February 28, 2021 22:01
-
-
Save yano3nora/a5e7e09c1ea4f63e40c84d08af95b561 to your computer and use it in GitHub Desktop.
[js: Count String] Count string occurrence in string. #js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ref. | |
// [JavaScript]文字列中に含まれるある文字の個数を簡単に返す。 | |
// https://qiita.com/simiraaaa/items/13b87190e9e1afc23e81 | |
/** | |
* countCharIn - 文字列中に含まれる特定文字の個数を返す | |
* | |
* @param string {string} | |
* @param character {[string, RegExp]} | |
* @return count {integer} | |
*/ | |
const countCharIn = (string, character) => { | |
return string.split(character).length - 1; | |
}; | |
console.log( | |
countCharIn('テストですテストテス', 'テス') // 3 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment