Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

StringTokenizer not helping in splitting

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

I am getting a set of strings with backslash (\) as the delimiter. These set of strings contain only values and they are in a set of sequence. The sequence helps us determine the keys of the particular value.

Such as

String s="s\\a\\b\\\\\\\\\\d";

I am using StringTokenizer to get the values. But its not working. StringTokenizer is not giving me the correct position. What should be the alternative in such a situation please suggest.

Thanks,
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Considering the variable s, what is expected output with StringTokenizer and what output are you getting?
 
Poornima Sharma
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Considering variable s what i am getting is :-

s
a
b
d

and what I am looking is

s
a
b




d
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
split method of String might help
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

StringTokenizer Javadoc wrote:StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

 
Poornima Sharma
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, this worked, thanks a lot. But the logic of doing this is not clear and why "\\" does not work here and "\\\\" works.
 
Swastik Dey
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you know \ is an escape character. So to denote one \ we need two \es and to denote two \es we need 4 \es. Correct me if I am wrong.
 
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Swastik Dey wrote:. . . Correct me if I am wrong.

You are correct. And you need to consider the \\ in both locations. Try thisand see how many \ you get printed out.

Also, in String#split() you are using a regular expression, and \ is an escape character there too. So "\\\\" matches the Sring \\ which is the regular expression to match \. But in the String "s\\a\\b\\\\\\\\\\d" you have one \ between s and a, so you need the regular expession \\, which is expressed as the String literal "\\\\".
 
reply
    Bookmark Topic Watch Topic
  • New Topic