java - Can't split text that contain "|" in String -


this question has answer here:

i'm wondering why cannot split text contain | separator in string. splitting works fine when use commas or like..

here ssce

package tests;  public class tests {             public static void main(string[] args) {          string text1="one|two|three|four|five";         string text2="one,two,three,four,five";          string [] splittedtext1 = text1.split("|");         string [] splittedtext2 = text2.split(",");          for(string elem : splittedtext1) system.out.println("text1="+elem);         for(string elem : splittedtext2) system.out.println("text2="+elem);     }     } 

any ideas why doesn't work "|" ??

since split(string regex) takes regex , | meta character, need escape it.

string[] splittedtext1 = splittedtext1.split("\\|"); 

or can use pattern class

a compiled representation of regular expression.

string[] splittedtext1 = splittedtext1.split(pattern.quote("|")); 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -