1

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.

4
  • Can't you just remove the comma? Commented Feb 10, 2014 at 5:21
  • If you don't know regex probably string.split array.splice and array.join Commented Feb 10, 2014 at 5:21
  • can you tell us the expected output? Your question is not clear. Commented 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. Commented Feb 10, 2014 at 5:28

2 Answers 2

1

use split function.

var str = "Architecture, Royal Melbourne Institute of Technology";
console.log(str.split(",")[0]);// logs Architecture

output array after splitting your string by , would have the expected result at the zeroth index.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

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.