0

I'm trying to change a huge string into the array of chars. In other languages there is .toCharArray(). I've used split to take dots, commas an spaces from the string and make string array, but I get only separated words and don't know how to make from them a char array. or how to add another regular expression to separate word? my main goal is something else, but I need this one first. thanks

var str = " If you don't pass anything, you'll get an array containing only  the original string, rather than an array containing each character."
str = str.toLowerCase();
str = str.split(/[ ,.]+/);
Levan Tsivadze
  • 73
  • 1
  • 2
  • 5
  • Possible duplicate of [How do you get a string to a character array in JavaScript?](http://stackoverflow.com/questions/4547609/how-do-you-get-a-string-to-a-character-array-in-javascript) – Rajesh Mar 10 '16 at 06:49
  • `split` method returns array of `words` itself, are you trying to make char array from each and every word inside that `str` array ? – tchelidze Mar 10 '16 at 06:50

8 Answers8

1
var charArray[];
for(var i = 0; i < str.length; i++) {
    charArray.push(str.charAt(i));
}

Alternatively, you can simply use:

var charArray = str.split("");
1

This will do:

var strAr = str.replace(/ /g,' ').toLowerCase().split("")
Tushar
  • 82,599
  • 19
  • 151
  • 169
Amrendra
  • 2,009
  • 2
  • 17
  • 36
1

You can use String#replace with regex and String#split.

arrChar = str.replace(/[', ]/g,"").split('');

Demo:

var str = " If you don't pass anything, you'll get an array containing only  the original string, rather than an array containing each character.";

var arrChar = str.replace(/[', ]/g,"").split('');

document.body.innerHTML = '<pre>' + JSON.stringify(arrChar, 0, 4) + '</pre>';

Add character in [] which you want to remove from string.

Tushar
  • 82,599
  • 19
  • 151
  • 169
RIYAJ KHAN
  • 14,597
  • 5
  • 30
  • 52
1

First you have to replace the , and . then you can split it:

var str = " If you don't pass anything, you'll get an array containing only  the original string, rather than an array containing each character."
var strarr = str.replace(/[\s,.]+/g, "").split("");

document.querySelector('pre').innerHTML = JSON.stringify(strarr, 0, 4)
<pre></pre>
Jai
  • 72,925
  • 12
  • 73
  • 99
0

I'm trying to change a huge string into the array of chars.

This will do

str = str.toLowerCase().split("");
gurvinder372
  • 64,240
  • 8
  • 67
  • 88
0

The split() method is used to split a string into an array of substrings, and returns the new array.

Tip: If an empty string ("") is used as the separator, the string is split between each character.

Note: The split() method does not change the original string.

Please read the link: http://www.w3schools.com/jsref/jsref_split.asp

BlackMamba
  • 9,652
  • 6
  • 42
  • 61
0

You may do it like this

var coolString,
    charArray,
    charArrayWithoutSpecials,
    output;

coolString = "If you don't pass anything, you'll get an array containing only  the original string, rather than an array containing each character.";

// does the magic, uses string as an array to slice
charArray = Array.prototype.slice.call(coolString);

// let's do this w/o specials
charArrayWithoutSpecials = Array.prototype.slice.call(coolString.replace(/[', ]/g,""))


// printing it here
output = "<b>With special chars:</b> " + JSON.stringify(charArray);
output += "<br/><br/>";
output += "<b>With special chars:</b> " + JSON.stringify(charArrayWithoutSpecials)
document.write(output);

another way would be

[].slice.call(coolString)
Ammar Hasan
  • 2,386
  • 14
  • 20
0

I guess this is what you are looking for. Ignoring all symbols and spaces and adding all characters in to an array with lower case.

var str = " If you don't pass anything, you'll get an array containing only  the original string, rather than an array containing each character."
str = str.replace(/\W/g, '').toLowerCase().split("");

alert(str);
sam
  • 2,127
  • 4
  • 17
  • 25