2

I have a string

|      859706 | Conficker infected host at 192.168.155.60    |        5744 |       7089 |        5 |                 4 | 1309714576 |
                1 | completed           | 

I need to split the using | which is nothing but pipe ( | ) symbol when i give the following split i get size of the array as 0

columns=parts[i].split('|');

where parts and columns are string arrays

4 Answers 4

6

| is a regex special character - you can escape it with backslash, so in java, you would write

columns=parts[i].split("\\|"); //first backslash escapes the second for java

EDIT: and if you need to support trailing empty columns, don't forget to use

columns=parts[i].split("\\|", -1);
1
  • that -1 options i was looking for +1 for it
    – Hunt
    May 29, 2020 at 10:23
1

I've had a similar issue and it worked with an escape char in the front i.e.

parts[i].split("\\|")
0

In split method use "[|]" instead "|".

0

you can try columns=parts[i].split("|");

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.