• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please help with split string into array  RSS feed

 
justin e wilson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a string that look like this
"dfhsdfhds | dsfdsfdsfd dsfjkhdskjf ||ER|| jdkshfuiewryuiwfhdsfsd er dsfjsgrwhfjkds ||ER|| jkshruiewryhijdknfjksdfhdksg | "

I want to split it by "||ER||"
i try this but it splits the single "|" to not just the "||ER||" like I want.

String[] separated = response.split("||ER||");
Log.d("token",separated[0]);
Log.d("token",separated[1]);
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The "|" has special meaning in a regular expression. It is the alternation operator. If you want to split on it, then you need to escape it with a backslash.

Henry
 
justin e wilson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
The "|" has special meaning in a regular expression. It is the alternation operator. If you want to split on it, then you need to escape it with a backslash.

Henry


can I do this with java? As in before it hit my splitting code.And if so do I just need to escape the single "|" or the "||ER||" too?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
justin e wilson wrote:
can I do this with java? As in before it hit my splitting code.And if so do I just need to escape the single "|" or the "||ER||" too?


Regular expressions provide multiple ways to escape the special meaning of regex operators. In this case, you can do it like this...



Or like this...



either way should work.

Henry
 
justin e wilson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
justin e wilson wrote:
can I do this with java? As in before it hit my splitting code.And if so do I just need to escape the single "|" or the "||ER||" too?


Regular expressions provide multiple ways to escape the special meaning of regex operators. In this case, you can do it like this...



Or like this...



either way should work.

Henry


Thank you that worked
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!