• Post Reply Bookmark Topic Watch Topic
  • New Topic

String conversion  RSS feed

 
Bandita Patel
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can I convert
String str = "Olivia||U|123|jko||||";

to

STring str 1= "Olivia| |U|123|jko| | | |";
 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to be replacing "||" with "| |".
Please look at String class's replaceAll() method.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind that| is a regex meta character and therefore needs to be escaped.
 
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
Surendra Kumar wrote:You seem to be replacing "||" with "| |".
Please look at String class's replaceAll() method.



Also, note that with replacing "||" with "| |", there are some edge conditions that need to be taken care of. For example, "||||" becomes "| || |", which may not be what you want.

Henry
 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Also, note that with replacing "||" with "| |", there are some edge conditions that need to be taken care of. For example, "||||" becomes "| || |", which may not be what you want.
Henry


Yes, the following works. I'm sure is not a clean way.
regex package will have better methods.

 
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
Surendra Kumar wrote:
Yes, the following works. I'm sure is not a clean way.
regex package will have better methods.




Two passes through the input string is probably not as efficient as a single pass. Try...



Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!