String to array filled with chars [duplicate] - javascript

This question already has answers here:
Split string into array [duplicate]
(10 answers)
Closed 5 years ago.
So I have a string similar to the following:
var mystring = "aasfeeeffdbals";
and I need to make it an array filled with the individual chars. I know what I can do that with the following method:
var charArray = []
for (var i = 0; i<mystring.length; i++) {
charArray.push(mystring[i]);
}
QUESTION: My understanding of a string is that it already in essence is an array of characters put together, so might there be a more straight forward way of creating a new array with the characters from the string?

var chars = "aasfeeeffdbals".split(''); will bring you wanted result

Related

How to split a string with arbitrary arguments? [duplicate]

This question already has answers here:
How can I split a string into segments of n characters?
(17 answers)
How do you use a variable in a regular expression?
(27 answers)
Closed 2 years ago.
I'm struggling with a very simple problem.
lines = "hogefoobarwai"
I want to cut this string into 4 characters.
Like this.
hoge, foob, arwa, i
How to split?
I try to use split() with regex.
let vars = lines.match(/.{4}/g);
This is good. But if something like {4} is variable, it won't work.
for example
length = 6
let vars = lines.match(/.{length}/g);
this shows literally /.{length}/.
If anyone can tell me what it is, please let me know.
You could take a minimum length of one (for getting smaller substrings) and the length and build a new regular expression.
const
lines = "hogefoobarwai",
length = 4,
parts = lines.match(new RegExp(`.{1,${length}}`, 'g'));
console.log(parts);
try using a dynamically generated Regex:
const newRegEx = new RegEx('{' + length + '}', g)

How to split a string using comma? [duplicate]

This question already has answers here:
How to split comma separated string using JavaScript? [duplicate]
(4 answers)
Closed 2 years ago.
I have a string like this
var assignments= "NAME1,NAME2,NAME3,NAME4,NAME5,NAME6"
how do i print only first three strings like this "NAME1,NAME2,NAME3" in JavaScript
You can do this way
let string= "NAME1,NAME2,NAME3,NAME4,NAME5,NAME6".split(',')
let output = string.splice(',',3).join(',')
console.log(output)
Using standard Array functions you can do what you want. The code below shows the various stages, though you can write it more concisely as one line if you like.
var assignments= "NAME1,NAME2,NAME3,NAME4,NAME5,NAME6"
var splitAssignements = assignments.split(",")
var firstThreeArray = splitAssignements.slice(0,3)
var firstThreeString = firstThreeArray.join(",")
console.log(firstThreeString)

Remove content after . from string [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 3 years ago.
I have a string var x = 2019-02-14T21:06:06.400Z
Another string y = 2019-02-14T21:06:06.44500Z
I need to remove content after the dot i cant slice it because the dot might come after 4 or 5 or 6 or n characters
Use split:
let [beforeDot] = "2019-02-14T21:06:06.400Z".split(".");
console.log(beforeDot);
You can use .split() in conjunction with an array access by index.
Example:
"2019-02-14T21:06:06.44500Z".split('.')[0]
A simple solution.
const strIn = "2019-02-14T21:06:06.44500Z";
const index = strIn.indexOf(".");
const strOut = strIn.substr(0, index);
console.log(strOut);

Javascript split number and string and keep both [duplicate]

This question already has answers here:
Javascript and regex: split string and keep the separator
(11 answers)
Splitting a string into chunks by numeric or alpha character with JavaScript
(3 answers)
Closed 6 years ago.
I have a string
var houseNumber = '13a';
I want to split the addition from the number so I can keep it in a other field.
How can I split this value without losing the number or the addition? At the end I would like to have 2 fields with the following type:
var houseNumber = 13; //Number
var addition = 'a'; //String
There are many questions about this, but I can't find one where both values has to be saved. That's why I created a new question.
Use the following code
var string_a = "jkjkhj89898";
var x = string_a.match(/[^\d]+|\d+/g);
console.log(x)
working fiddle.
Thanks

JavaScript / jQuery: how to split string with multiple values [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 8 years ago.
I have a string that always contains 8 variable values that are separated with a hypen (-) like in the following example:
5-2-2-2-2-2-2-1
What is the best way to split this into 8 separate values so that I can use each of them in a variable if the values can be either an integer or the value 'n/a' ?
Many thanks for any help with this, Tim.
var str = '5-2-2-2-2-2-2-1';
var parts = str.split('-');
for (var i=0;i<parts.length;i++){
console.log(parts[i]);
}
You are searching for the String.split() method

Categories

Resources