2

i have data in

var description="Name:John;EmployeeID:2;Salary:8000ドル;Address:London";

i want the result as

Name: John
Employee Id: 2
Salary: 8000ドル
Address: London

is it possible with split() function in javascript?

Shakti Singh
86.8k21 gold badges142 silver badges156 bronze badges
asked Jun 23, 2011 at 12:17
0

6 Answers 6

5

You can do it with String.split() but in this case it's simpler to use String.replace():

var description="Name:John;EmployeeID:2;Salary:8000ドル;Address:London";
description = description.replace(/;/g, '\n').replace(/:/g, ': ');
/*
"Name: John
EmployeeID: 2
Salary: 8000ドル
Address: London"
*/
answered Jun 23, 2011 at 12:20
Sign up to request clarification or add additional context in comments.

Comments

2

If you want the result as an object, try:

var f = function (str) {
 var x = {}, key2label = { EmployeeID: 'Employee Id' };
 str.replace(/(.+?):(.+?)(;|$)/g, function (match, key, value) {
 key = key2label[key] || key;
 x[key] = value;
 });
 return x;
};

If a simple string is needed, but you still need to replace keys:

var f2 = function (str) {
 var key2label = { EmployeeID: 'Employee Id' };
 return str.replace(/(.+?):(.+?)(;|$)/g, function (match, key, value, semi) {
 key = key2label[key] || key;
 return key + ': ' + value + (semi ? '\n' : '');
 });
};

If you really didn't mean to replace keys, this will do it:

var f3 = function (str) {
 return str.split(':').join(': ').split(';').join('\n');
};

... or use Matt Ball's answer.

answered Jun 23, 2011 at 12:27

Comments

1

With this statement:

var arrDescription = description.split(";");

you will get an array with all the values. For more info on split check the following link.

you can even join them afterwards :

printf(arrDescription.join(" "));

For more info on join check the following link.

Max

Jacob George
2,6271 gold badge18 silver badges28 bronze badges
answered Jun 23, 2011 at 12:21

4 Comments

how can i find a substring from arrDescription
i have only substring not the complete string
if i understand well, you want to do a search in your array with substring only. you could do a loop on your array but there might be a more efficient way so i advise you to ask another question so that javascript gurus will provide you with the best answers
1

You can probable try like this to display.

 var description="Name:John;EmployeeID:2;Salary:8000ドル;Address:London";
 var arr=new Array();
 arr=description.split(";"); 
 for(var i=0;i<arr.length;i++)
 document.writeln("<h4>"+arr[i]+"</h4>"); 
answered Jun 23, 2011 at 12:42

2 Comments

if i have description in textBox then how can i use split function.
You can get the description from textBox using document.getElementById("textBoxId").value
0

Yes.

You first should split on the semicolon ;. Loop through those results, and split each result on each colon :.

You will have to build the result by hand.

answered Jun 23, 2011 at 12:20

Comments

0

var description="Name:John;EmployeeID:2;Salary:8000ドル;Address:London"; var splitted = description.split(";");

for(var i = 0; i < splitted.length; i++) { document.write(splitted[i] + ""); }

answered Jul 4, 2013 at 14:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.