Dar
Dar

Reputation: 11

How could I use a character to split a string into multiple?

I am using node.js and I was wondering how I could do this with a string

starting with something like "ex1:ex2:ex3"

var rl = require('readline').createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question('Enter a combo (1:2:3) : ', (answer) => {


});

How could I turn an input like "ex1:ex2:ex3" into individual variables that contain "ex1", "ex2", and "ex3"

Upvotes: 1

Views: 45

Answers (1)

Abhishek Jha
Abhishek Jha

Reputation: 153

you can use the split function

var split_string = answer.split(":");
      
console.log(split_string);

Result:

["ex1", "ex2", "ex3"]

now you can use a for loop to iterate over this array.

Upvotes: 1

Related Questions