Prady
Prady

Reputation: 11330

Splitting a string using '|'

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

Upvotes: 2

Views: 2341

Answers (4)

Ilya Saunkin
Ilya Saunkin

Reputation: 19820

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

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

Upvotes: 1

Paul Bellora
Paul Bellora

Reputation: 55233

| 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);

Upvotes: 6

Rasel
Rasel

Reputation: 15477

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

Upvotes: 0

sudmong
sudmong

Reputation: 2036

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

Upvotes: 0

Related Questions