最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How do I separate a comma delimited variable into multiple variables? - Stack Overflow

matteradmin7PV0评论

This is probably very elementary, but I'm still learning.
I've imported a record from an XML file and have the result :
"a,b,c,d,e,f,g,h"

I would like to end up with 8 separate variables, one for each ma delimited value.
What is the shortest way to code this using javascript?

This is probably very elementary, but I'm still learning.
I've imported a record from an XML file and have the result :
"a,b,c,d,e,f,g,h"

I would like to end up with 8 separate variables, one for each ma delimited value.
What is the shortest way to code this using javascript?

Share Improve this question edited Jun 25, 2013 at 19:30 Taryn 248k57 gold badges371 silver badges408 bronze badges asked Dec 2, 2010 at 17:14 KirtKirt 2,7232 gold badges18 silver badges11 bronze badges 2
  • 1 do you mean split? ~ Further learning: developer.mozilla/en/JavaScript/Reference/Global_Objects/… – jcolebrand Commented Dec 2, 2010 at 17:15
  • Yes, SPLIT, now a term I will never forget. I'm still learning the proper terminology. The theory is always so clear in my head...I just jumble syntax in the transcription, ha! – Kirt Commented Dec 2, 2010 at 18:48
Add a ment  | 

4 Answers 4

Reset to default 4

if you .split() the list, you'll end up with a single array with 'n' number of elements. Not -exactly- 8 separate variables, but 8 elements that can be accessed individually.

var myList = "a,b,c,d,e,f,g,h";
var myArray = myList.split( ',' );

alert( myArray[ 4 ] );

use split()

js>s = "a,b,c,d,e,f,g,h"
a,b,c,d,e,f,g,h
js>a = s.split(',')
a,b,c,d,e,f,g,h
js>a[0]
a
js>a[4]
e

Use split().

In your case, xml_string.split(',');

If you have the result as a string, use the split method on it:

var myList = "a,b,c,d,e,f,g,h";
var myArray = myList.split(",");
Post a comment

comment list (0)

  1. No comments so far