Reputation: 582
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.
Upvotes: 1
Views: 5336
Reputation: 138
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.
Upvotes: 0
Reputation: 27845
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.
Upvotes: 1