user10044012
user10044012

Reputation:

Split not displaying in current format

I have this string:

var mystring = "save 'myfile.txt' 'this is the content'";

I need it to break like this:

var command = mystring[0];
var filename = mystring[1];
var content = mystring[2];

I am using this:

var mystring = message.content.split("'' ,");

This is not showing anything

How can I do this?

Upvotes: 1

Views: 42

Answers (3)

Nina Scholz
Nina Scholz

Reputation: 386560

You could match the parts and use a destructuring assignment.

var string = "save 'myfile.txt' 'this is the content'",
    [, command, filename, content] = string.match(/(.*) '(.*)' '(.*)'/);
    
console.log(command);
console.log(filename);
console.log(content);

Upvotes: 1

trincot
trincot

Reputation: 350167

You could use a regular expression for your split: mystring.split(/'?\s*'/)

var mystring = "save 'myfile.txt' 'this is the content'";
var [command, filename, content] = mystring.split(/'?\s*'/);
console.log({command, filename, content});

Upvotes: 1

Matei Adriel
Matei Adriel

Reputation: 94

Try:

const mystring = message.content.split(" ");
const command = mystring[0];
const filename = mystring[1].substr(1,mystring[1].length - 2);
const content = mystring[2].substr(1,mystring[2].length - 2);

Upvotes: 0

Related Questions