Skip to content

Instantly share code, notes, and snippets.

@yano3nora
Last active February 28, 2021 22:01
Show Gist options
  • Save yano3nora/a5e7e09c1ea4f63e40c84d08af95b561 to your computer and use it in GitHub Desktop.
Save yano3nora/a5e7e09c1ea4f63e40c84d08af95b561 to your computer and use it in GitHub Desktop.
[js: Count String] Count string occurrence in string. #js
// 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