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

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

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

android - How to create dynamically Fragment pager adapter -