LoginSignup
4
4

More than 5 years have passed since last update.

JavaScript Stringオブジェクトまとめ ~走れメロス~

Last updated at Posted at 2017-04-22

なんとなく使っていた主なStringオブジェクトをまとめてみました。

Stringオブジェクト

検索

メンバー 概要
1 indexOf(substr[,start]) 文字列前方(start はindex)から部分文字列substrを検索
2 lastIndexOf(substr[,start]) 文字列後方(start はindex)から部分文字列substrを検索
3 startsWith(search[,position]) *ES6 文字列が指定された部分文字列searchで始まるか(引数positionは検索開始位置)
4 endsWith(search[,position]) *ES6 文字列が指定された部分文字列searchで終わるか引数positionは文字列が指定した長さであるかのように文字列内の検索を行う
5 includes(search[,position]) *ES6 文字列に指定された部分文字列searchが含まれるか

code

script.js
var str1 = "メロスは激怒した。必ず、かの邪智暴虐の王を除かなければならぬと決意した。メロスには政治がわからぬ。";

// 1 indexOf(substr[,start]) 文字列前方(start はindex)から部分文字列substrを検索 
console.log(srt1.indexOf('メロス'));//0
console.log(str1.indexOf('メロス',3));//36
console.log(str1.indexOf('メロン'));//-1 不一致


// 2 lastIndexOf(substr[,start]) 文字列後方(startはindex)から部分文字列substrを検索
console.log(str1.lastIndexOf('メロス'));//36
console.log(str1.lastIndexOf('メロス',30));//0


// 3 startsWith(search[,position]) *ES6 文字列が指定された部分文字列searchで始まるか(引数positionは検索開始位置)
console.log(str1.startsWith("メロス"));//true
console.log(str1.startsWith("メロス",1));//false
console.log(str1.startsWith("ロス",1));//true


// 4 endsWith(search[,position]) 文字列が指定された部分文字列searchで終わるか (引数positionは文字列が指定した長さであるかのように文字列内の検索を行う)
console.log(str1.endsWith("わからぬ。"));//true
console.log(str1.endsWith("メロス"));//false
console.log(str1.endsWith("メロス",3));//true


// 5 includes(search[,position]) *ES6 文字列に指定された部分文字列searchが含まれるか
console.log(str1.includes("メロス")); //true
console.log(str1.includes("メロン")); //false

部分文字列

メンバー 概要
1 charAt(n) n+1(index)の文字を抽出
2 slice(start[,end]) 文字列からstart+1(index)~end文字列を抽出
3 substring(start[,end]) 文字列からstart+1(index)~end文字列を抽出 
4 substr(start[,cnt]) 文字列のstart+1(index)からcnt文字を抽出
5 split(str[,limit]) 文字列を分割文字列strで分割し、その結果を配列として取得(引数limitは最大分割数)

code

script.js
var str1 = "メロスは激怒した。必ず、かの邪智暴虐の王を除かなければならぬと決意した。メロスには政治がわからぬ。";
var str2 = "メロスは、村の牧人である。";

// 1 charAt(n) n+1(index)の文字を抽出
console.log(str2.charAt(5));//村


// 2 slice(start[,end]) 文字列からstart+1(index)~end文字列を抽出 
console.log(str2.slice(5,9));//村の牧人


// 3 substring(start[,end]) 文字列からstart+1(index)~end文字列を抽出 
console.log(str2.substring(5,9));//村の牧人


// 4 substr(start[,cnt])|文字列のstart+1(index)からcnt文字を抽出
console.log(str2.substr(5,4));//index番号5から4文字抽出


// 5 split(str[,limit]) 文字列を分割文字列strで分割し、その結果を配列として取得(引数limitは最大分割数)
console.log(str2.split("")); //["メロスは", "村の牧人である。"]
console.log(str1.split("")); //["メロス", "激怒した。必ず、かの邪智暴虐の王を除かなければならぬと決意した。メロスに", "政治がわからぬ。"]
console.log(str1.split("",1)); //["メロス"]
console.log(str1.split("",2)); //["メロス", "激怒した。必ず、かの邪智暴虐の王を除かなければならぬと決意した。メロスに"]

大文字⇆小文字

メンバー 概要
1 toLowerCase() 小文字に変換
2 toUpperCase() 大文字に変換

code

script.js
var str3 = "Run, Melos, Run"

// 1 toLowerCase() 小文字に変換
console.log(str3.toLowerCase());//run, melos, run


// 2 toUpperCase() 大文字に変換
console.log(str3.toUpperCase());//RUN, MELOS, RUN


コード変換

メンバー 概要
1 charCodeAt(index) 与えれたインデックス位置の文字の UTF-16 コードユニット値を表す数値を返す。index が範囲外だった場合、NaNを返す。
2 String.fromCharCode(num1[, ...[, numN]]) Unicode の値の数のシーケンスに対応した文字を含む文字列を返す

code

script.js
var str4 = "Melos"

// 1 charCodeAt(index)|与えれたインデックス位置の文字の UTF-16 コードユニット値を表す数値を返す。index が範囲外だった場合、NaNを返す。
console.log(str4.charCodeAt(0));//77 
console.log(str4.charCodeAt(1));//101 
console.log(str4.charCodeAt(2));//108
console.log(str4.charCodeAt(3));//111
console.log(str4.charCodeAt(4));//115
console.log(str4.charCodeAt(5));//NaN


// 2 String.fromCharCode(num1[, ...[, numN]]) Unicode の値の数のシーケンスに対応した文字を含む文字列を返す
console.log(String.fromCharCode(77,101,108,111,115));//Melos

その他

メンバー 概要
1 concat(str) 文字列の後方にstrを連結
2 repeat(num) *ES6 文字列をnum回だけ繰り返したものを取得
3 trim() 文字列の前後から空白を削除 
4 length 文字列の長さを取得

code

script.js
var str1 = "メロスは激怒した。必ず、かの邪智暴虐の王を除かなければならぬと決意した。メロスには政治がわからぬ。";
var str2 = "メロスは、村の牧人である。";
var str5 = "  メロス   ";

// 1 concat(str) 文字列の後方にstrを連結
console.log(str1.concat(str2));//メロスは激怒した。必ず、かの邪智暴虐の王を除かなければならぬと決意した。メロスには政治がわからぬ。メロスは、村の牧人である。

// 2 repeat(num) *ES6 文字列をnum回だけ繰り返したものを取得 
console.log(str2.repeat(3));//メロスは、村の牧人である。メロスは、村の牧人である。メロスは、村の牧人である。


// 3 trim() 文字列の前後から空白を削除
console.log(str5.trim());//メロス


// 4 length 文字列の長さを取得
console.log(str1.length);//49


以上になります。
正規表現は分量が多くなるので別にまとめようと思います。
閲覧ありがとうございました。

走れメロス

4
4
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
4
4