How to split a string using comma? [duplicate] - javascript

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)

Related

how to convert "12A,13B,14C" to [12A,13B,14C] in javascript [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 5 years ago.
I have a string like below.
sections = "12A,13B,14C"
But I need [12A, 13B, 14C] for future use. How to create array from above string?
You have to use split method for that to split your string data into array values, have a look to updated code
var sections = "12A,13B,14C";
sections = sections.split(',');
console.log(sections);
Hope this will help! Just use split method of JS.
sections = "12A,13B,14C"
let array = sections.split(',');
console.log(array)

Regex after first sign [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 5 years ago.
I'm gonna to write regex or other expression to get coordinates after '='.
My example:
var cords = https://maps.googleapis.com/maps/api/staticmap?center=50.082961,19.966860&zoom=13&size=300x300&sensor=false&markers=color:orange%7C50.082961,19.966860&client=gme-marktplaats&channel=bt_pl&signature=lPDQWiNQ2_mY8xgoVthZHLLYWac=
I want to get 50.082961,19.966860
I know that I could use slice but I think I could write it better with regex.
Simple base for this example: \=(.[0-9]) What's next?
Try this center\=(\d+\.\d+,\d+\.\d+)&
var val = 'https://maps.googleapis.com/maps/api/staticmap?center=50.082961,19.966860&zoom=13&size=300x300&sensor=false&markers=color:orange%7C50.082961,19.966860&client=gme-marktplaats&channel=bt_pl&signature=lPDQWiNQ2_mY8xgoVthZHLLYWac='.match(/center\=(\d+\.\d+,\d+\.\d+)&/)[1]
console.log(val)
But as other's have commented, you likely shouldn't be using regex for this purpose

String to array filled with chars [duplicate]

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

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