Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

doubt in string.split method

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

I have following data:

1||1||Abdul-Jabbar||Karim||1996||1974

I want to delimit the tokens.

Here the delimiter being "||".

My delimiter setter is:



However, String[] tokens = line.split(delimiter); is not giving required result.

Please help!!

Nik
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why would you start with "\"" and end with "\"" ?
and what if your delimiter doesn't need escape character?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you explain what you are trying to do in setDelimiter method?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bit tricky. But still, it should not take much time to resolve.

Assuming that you came here for advise on the possible delimeters, let me know if the below code did helped you.

 
vicky chauhan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roshan Ramesh wrote:Bit tricky. But still, it should not take much time to resolve.

Assuming that you came here for advise on the possible delimeters, let me know if the below code did helped you.




Here is how I did it:

String delimiter = "||";
Pattern pattern = Pattern.compile(Pattern.quote(delimiter));
String[] tokens = pattern.split(line);

This worked perfectly... and is more efficient too!! Thanks to Ray!!
 
vicky chauhan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raymond Tong wrote:
Why would you start with "\"" and end with "\"" ?
and what if your delimiter doesn't need escape character?



Thanks Raymond.. I found a solution.. I have posted it in the last comment in this post..
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic