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
  • 84,385
  • 21
  • 134
  • 153
Saloni
  • 525
  • 2
  • 12
  • 31

6 Answers6

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"
*/
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
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.

Bergius
  • 949
  • 6
  • 14
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,559
  • 1
  • 16
  • 28
JMax
  • 26,109
  • 12
  • 69
  • 88
  • how can i find a substring from arrDescription – Saloni Jun 23 '11 at 12:28
  • @Saloni: are you trying to find an item in an array ? --> http://stackoverflow.com/questions/143847/best-way-to-find-an-item-in-a-javascript-array or http://stackoverflow.com/questions/237104/javascript-array-containsobj – JMax Jun 23 '11 at 12:30
  • i have only substring not the complete string – Saloni Jun 23 '11 at 12:34
  • 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 – JMax Jun 23 '11 at 12:41
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>");    
Srikanth Venkatesh
  • 2,812
  • 1
  • 20
  • 13
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] + ""); }

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.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445