Created
November 29, 2011 08:14
-
-
Save shinout/1403979 to your computer and use it in GitHub Desktop.
split strings with spaces
This file contains hidden or 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
function idxOf(str) { | |
var results = []; | |
var idx = str.indexOf(' '); | |
while (idx >= 0 && str.length) { | |
if (idx > 0) { | |
results.push(str.slice(0, idx)); | |
} | |
str = str.slice(idx + 1); | |
idx = str.indexOf(' '); | |
} | |
if (str.length) { | |
results.push(str); | |
} | |
return results; | |
} | |
var str = " hoge fuga aaaa にほんごーーーーー あああああ ??"; | |
console.log(idxOf(str)) |
This file contains hidden or 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
function recursive(str) { | |
var idx = str.indexOf(' '); | |
if (idx < 0) { | |
return (str) ? [str] : []; | |
} | |
else if (idx == 0) { | |
return recursive(str.slice(idx + 1)); | |
} | |
return [str.slice(0, idx)].concat(recursive(str.slice(idx + 1))); | |
} | |
var str = " hoge fuga aaaa にほんごーーーーー あああああ ?? "; | |
console.log(recursive(str)) |
This file contains hidden or 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
function reg2(str) { | |
var regex = new RegExp('([^ ]+) ?', 'g'), | |
results = [], | |
result; | |
while (result = regex.exec(str)) { | |
results.push(result[1]); | |
} | |
return results; | |
} | |
var str = " hoge fuga aaaa にほんごーーーーー あああああ ?? "; | |
console.log(reg2(str)) |
This file contains hidden or 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
var str = "h oge fuga yeah commF031 fasd93+++. 日本語もいけちゃう ? s "; | |
function srch(str) { | |
// search | |
var curchars = []; | |
var res = []; | |
for (var i=0, l=str.length; i<l; i++) { | |
var ch = str.charAt(i); | |
if (ch == ' ') { | |
if (curchars.length) { | |
res.push(curchars.join('')); | |
curchars = []; | |
} | |
} | |
else { | |
curchars.push(ch); | |
} | |
} | |
if (curchars.length) { | |
res.push(curchars.join('')); | |
} | |
return res; | |
} | |
function automaton(str) { | |
const START = 0; | |
const NOT_EMPTY = 1; | |
const END = 2; | |
var l = str.length; | |
var state = START; | |
var results = []; | |
var current = ''; | |
var i = 0; | |
while (state != END) { | |
state = transition(state, i++); | |
} | |
return results; | |
function transition(state, i) { | |
if (i >= str.length) { | |
if (state == NOT_EMPTY) { | |
results.push(current); | |
} | |
return END; | |
} | |
var ch = str.charAt(i); | |
var isSpace = ch == ' '; | |
switch (state) { | |
case START: | |
if (isSpace) return START; | |
current += ch; | |
return NOT_EMPTY; | |
case NOT_EMPTY: | |
if (isSpace) { | |
results.push(current); | |
current = ''; | |
return START; | |
} | |
current += ch; | |
return NOT_EMPTY; | |
case END: | |
return END; | |
} | |
} | |
} | |
function spl(str) { | |
// split | |
return str.trim().split(/ +/) | |
} | |
function reg(str) { | |
// regexp | |
return str.match(/[^ ]+/g) || []; | |
} | |
console.log(srch(str)) | |
console.log(automaton(str)) | |
console.log(spl(str)) | |
console.log(reg(str)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment