###実現したいこと
文字列の中から,"指定した文字"を見つけ出し任意の情報を抽出.
###該当のソースコード
JavaScript
1 <script type="text/javascript"> 2 function Info(x,y){ 3 this.x = +x; 4 this.y = +y; 5 } 6 7 var str = "6f72696f6e" , infoList = []; 8 for(var i=0,len=str.length,x,y ; i<len ; ++i){ 9 if(str[i] === "6" && str[i+1] === "f"){ 10 x = str[++i+1]; 11 y = str[++i+1]; 12 infoList.push = (newInfo(x,y)); 13 console.log(x); 14 console.log(y); 15 } 16 }
Javascript
1 <script type="text/javascript"> 2 function Info(x,y){ 3 this.x = +x; 4 this.y = +y; 5 } 6 7 var lastIndex = -1; 8 var str = "6f72696f6e" , infoList = []; 9 10 while(true){ 11 var x,y; 12 var nextIndex = str.indexOf("6f",lastIndex+1); 13 if(nextIndex === -1){ 14 break; 15 }else{ 16 x = str[nextIndex+2]; 17 y = str[nextIndex+3]; 18 infoList.push = (newInfo(x,y)); 19 console.log(x); 20 console.log(y); 21 lastIndx = nextIndex; 22 } 23 }
###質問したいこと
strの中身が何百万になるとしたら,どちらの方法が良いのでしょうか?
それともほかに何か方法がありますでしょうか.
(指定する文字は6fだけではありません.)
以上よろしくお願いします.
###補足情報(言語/FW/ツール等のバージョンなど)
言語 JavaScriptのみ
###追記・修正
12/26 20:47 SyntaxError修正
回答2件
あなたの回答
tips
プレビュー