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

8 Answers 8

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.

Sign up to request clarification or add additional context in comments.

Comments

1

This will do:

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

Comments

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>

Comments

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

Comments

0

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

This will do

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

Comments

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

Comments

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)

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.