admin管理员组文章数量:1200344
I want a string split at every single character and put into an array. The string is:
var string = "hello";
Would you use the .split()
? If so how?
I want a string split at every single character and put into an array. The string is:
var string = "hello";
Would you use the .split()
? If so how?
- Do you really mean "certain character numbers" as specified in the title , or at "every single character" as in the body of the question? – jball Commented Oct 25, 2010 at 18:40
- I thought they would be related. Because I was thinking it would be every 1 character in this case, but in other cases it would be maybe every three characters. I was going for a broader question that I thought was related. – chromedude Commented Oct 25, 2010 at 18:44
- 1 The solution to the "certain number" yields a solution to "every single", but the reverse is not necessarily true. For example @Harmen's answer below would be a strange starting point for splitting a string into 3 character chunks. – jball Commented Oct 25, 2010 at 18:51
7 Answers
Reset to default 10I was researching a similar problem.. to break on every other character. After reading up on regex, I came up with this:
data = "0102034455dola";
arr = data.match(/../g);
The result is the array: ["01","02","03","44","55","do","la"]
Yes, you could use:
var str = "hello";
// returns ["h", "e", "l", "l", "o"]
var arr = str.split( '' );
If you really want to do it as described in the title, this should work:
function splitStringAtInterval (string, interval) {
var result = [];
for (var i=0; i<string.length; i+=interval)
result.push(string.substring (i, i+interval));
return result;
}
var s= "hello";
s.split("");
Here is a simple way do it with a while loop;
function splitStringAtInterval(str, len){
var len = 10;
var arr = [];
str = str.split("");
while(str.length > len){
arr.push(str.splice(position,len).join(""));
}
if(str.length > 0)arr.push(str.join(""));
return arr;
}
If you want it short and 'functional':
var input = 'abcdefghijklmn1234567890';
var arr = Array.prototype.slice.call(input), output = [];
while (arr.length) output.push(arr.splice(0, 3).join(''));
output; // ["abc", "def", "ghi", "jkl", "mn1", "234", "567", "890"]
If you don't want to use a RegExp, you can also do this instead:
const splitEvery = (nth) => (str) =>
Array.from(
{length: Math.ceil(str.length / nth)},
(_, index) => str.slice(index * nth, (index + 1) * nth)
)
// example usage:
const splitEvery2nd = splitEvery(2)
const result = splitEvery2nd('hello')
// result is: ['he', 'll', 'o']
If you want to cut off any remaining parts, replace the Math.ceil
with a Math.floor
call.
Understanding:
This function creates an Array with the length of the number of slices, containing the expected parts of the text.
本文标签: How do you split a string at certain character numbers in javascriptStack Overflow
版权声明:本文标题:How do you split a string at certain character numbers in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738546057a2096441.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论