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?

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"
*/
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.

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

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>");    

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.

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] + ""); }

Comments

Your Answer

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.