I'm newbie in Node JS. How can I manipulate this sentence "Architecture, Royal Melbourne Institute of Technology.", I just want to put words before comma (in this case Architecture)? So if I find comma, I'll put all words before the first comma.
-
Can't you just remove the comma?salezica– salezica2014-02-10 05:21:22 +00:00Commented Feb 10, 2014 at 5:21
-
If you don't know regex probably string.split array.splice and array.joinBenjamin Gruenbaum– Benjamin Gruenbaum2014-02-10 05:21:49 +00:00Commented Feb 10, 2014 at 5:21
-
can you tell us the expected output? Your question is not clear.Mithun Satheesh– Mithun Satheesh2014-02-10 05:24:31 +00:00Commented Feb 10, 2014 at 5:24
-
@mithunsatheesh input = "Architecture, Royal Melbourne Institute of Technology". output = "Architecture". The logic: find first comma and put words before it.Nizamil Putra– Nizamil Putra2014-02-10 05:28:13 +00:00Commented Feb 10, 2014 at 5:28
Add a comment
|
2 Answers
Its again a normal Javascript, all the methods can be used in nodeJS. var name = "any string"; For example:
var str = "Hi, world",
arrayOfStrings = str.split(','),
output = arrayOfStrings[0]; // output contains "Hi"
You can update the required field by directly replacing the string ie.
arrayOfStrings[0] = "other string";
str = arrayOfStrings.join(' '); // "other string world"
Point to be noted: If we update the output, as we are updating the string it contains the copy NOT the reference, while joining it gives the same text ie, "Hi world". So we need to change the reference value ie arrayOfStrings[0] then .join(' ') will combine the required string.