• Post Reply Bookmark Topic Watch Topic
  • New Topic

append a space if found dbl separator  RSS feed

 
Maya Choy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
file.txt
a~b~c~~e

StringTokenizer st = new StringTokenizer(line,"~");

if found "~~" how do i append a space on it?
eg a~b~c~ ~e

tks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maya,

This forum is devoted to JSP issues.
I'll move this thread to the Java In General (beginner) forum which is the best place to ask about string manipulation in Java.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure exactly what you're trying to accomplish, but I suspect you ran into the problem that StringTokenizer doesn't usually tell you if it found two delimiters in a row, so it's hard to detect zero-length substrings.

* Look into String.replaceAll() to replace "~~" with "~ ~" before splitting.

* Or look at the optional parameter on StringTokenizer that will give you all the delimiters as well as the values between them. Then you can detect two delimiters in a row even without the space.

* And see what String.split() does with two delimiters in a row.

Let us know if one of those matched your problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!