Levan Tsivadze
Levan Tsivadze

Reputation: 83

Javascript: String of text to array of characters

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(/[ ,.]+/);

Upvotes: 0

Views: 4056

Answers (8)

Shashank Saxena
Shashank Saxena

Reputation: 123

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

Alternatively, you can simply use:

var charArray = str.split("");

Upvotes: 1

Jai
Jai

Reputation: 74738

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>

Upvotes: 1

sam
sam

Reputation: 2345

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);

Upvotes: 0

Amrendra
Amrendra

Reputation: 2077

This will do:

var strAr = str.replace(/ /g,' ').toLowerCase().split("")

Upvotes: 1

Ammar Hasan
Ammar Hasan

Reputation: 2516

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)

Upvotes: 0

RIYAJ KHAN
RIYAJ KHAN

Reputation: 15292

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.

Upvotes: 1

BlackMamba
BlackMamba

Reputation: 10254

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

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68393

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

This will do

str = str.toLowerCase().split("");

Upvotes: 0

Related Questions