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
  • 83
  • 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
  • 85,780
  • 21
  • 159
  • 179
Amrendra
  • 2,019
  • 2
  • 18
  • 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
  • 85,780
  • 21
  • 159
  • 179
RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
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
  • 74,255
  • 12
  • 74
  • 103
0

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

This will do

str = str.toLowerCase().split("");
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
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
  • 10,054
  • 7
  • 44
  • 67
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,436
  • 16
  • 22
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,277
  • 4
  • 18
  • 25