1

didn't find the solution in SPLIT function.. i was trying to convert a string into array.. String is like.

My name-- is ery and your-- is this 

i just want to convert that string to array, and then print it out but while getting this '-- ' break the line too.

i have did that so far

function listToAray(fullString, separator) { var fullArray = []; if (fullString !== undefined) { if (fullString.indexOf(separator) == -1) { fullAray.push(fullString); } else { fullArray = fullString.split(separator); } } return fullArray; } 

but is for Comma separated words in string, but what i want is to just convert string to array and then print it out while breaking line on getiing '-- ' this is array Thanks in advance

1
  • 1
    The question title mentions the other way round. Commented Sep 3, 2014 at 10:34

3 Answers 3

1

seems to work :

text = "My name-- is ery and your-- is this"; function listToAray(fullString, separator) { var fullArray = []; if (fullString !== undefined) { if (fullString.indexOf(separator) == -1) { fullAray.push(fullString); } else { fullArray = fullString.split(separator); } } return fullArray; } console.log(listToAray(text,"--")); 

console output:

["My name", " is ery and your", " is this"] 

what do you expect ?

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

1 Comment

In fact, you can use only fullArray = fullString.split(separator); inside the if statement because if separator is not in the string the split function will convert the string into an array anyway
0

Why are you doing all that complicated stuff man? There is a .split() method that allows you to do that in a single line of code:

text = "My name-- is ery and your-- is this"; array = text.split('--'); > ["My name", " is ery and your", " is this"] 

Now if you want to break line on -- then you can do the following:

text = "My name-- is ery and your-- is this"; list = text.replace(/\-\-/g, '\n'); console.log(list); > "My name is ery and your is this" 

Comments

0

You can use the split method:

var str = "My name-- is ery and your-- is this"; var res = str.split("--"); console.log(res); // console output will be: ["My name", " is ery and your", " is this"] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.