0

I have not been coding for long and ran into my first issue I just can not seem to figure out.

I have a string "XX|Y1234$ZT|QW4567" I need to remove both $ and | and push it into an array like this ['XX', 'Y1234', 'ZT', 'QW4567'].

I have tried using .replace and .split in every way I could like of

 var array = "XX|Y1234$ZT|QW4567"
 var array2 = [];

 array = array.split("$");

 for(i = o; i <array.length; i++)    
   var loopedArray = array[i].split("|")
   loopedArray.push(array2);        
 } 

I have tried several other things but would take me awhile to put them all down.

1
  • take care with your code, your for loop doesn't have a { (opening brace) but have a } (closing brace) this will cause issues. also i = o didn't you mean var i = 0 ? Also, array2 is an empty array that you never modify, there's no logic in pushing it inside loopedArray Commented Feb 21, 2020 at 21:28

4 Answers 4

3

You can pass Regex into .split(). https://regexr.com/ is a great tool for messing with Regex.

// Below line returns this array ["XX", "Y1234", "ZT", "QW4567"]
// Splits by $ and |
"XX|Y1234$ZT|QW4567".split(/\$|\|/g); 
Sign up to request clarification or add additional context in comments.

2 Comments

I guess, .split(/[\$\|]/g) scales more nicely, when there're lot of delimiters, however upvoted
@YevgenGorbunkov Tipp: You don't even have to escape | and $ inside a character class. And as we're not operating the regex to match blocks by itself, the g-match-all-flag isn't needed too. .split(/[|$]/) should be enough. But i'm with you about your initial statement: This should be faster.
1

Your code snippet is close, but you've messed up your variables in the push statement.

var array = "XX|Y1234$ZT|QW4567"
var array2 = [];

array = array.split("$");

for (i = 0; i < array.length; i++) {
  var loopedArray = array[i].split("|")
  array2.push(loopedArray);
}
array2 = array2.flat();

console.log(array2);

However, this can be rewritten much cleaner using flatMap. Also note the use of let instead of var and single quotes ' instead of double quotes ".

let array = 'XX|Y1234$ZT|QW4567'
let array2 = array
  .split('$')
  .flatMap(arrayI => arrayI.split('|'));

console.log(array2);

And lastly, split already supports multiple delimiters when using regex:

let array = 'XX|Y1234$ZT|QW4567'
let array2 = array.split(/[$|]/);

console.log(array2);

Comments

0

You can do this as follows:

"XX|Y1234$ZT|QW4567".replace('$','|').split('|')

It will produce the output of:

["XX", "Y1234", "ZT", "QW4567"]

1 Comment

worked great. thats a simple great answer thank you for the help.
0

If you call the split with two parameters | and the $ you will get an strong array which is splittend by the given characters.

var array = "XX|Y1234$ZT|QW4567";
var splittedStrings = array.Split('|','$');
foreach(var singelString in splittedStrings){
   Console.WriteLine(singleString);
}

the output is:

XX
Y1234
ZT
QW4567

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.