Win a copy of Svelte and Sapper in Action this week in the JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

splitting a string with multiple delimiters

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

how to split a string based on multiple delimiting characters.

for example i have these delimiters in string array---->String[] sep_list = { " ", "\n","[","]","{","}","(",")"};

how can we split it in one go...
 
Sheriff
Posts: 22001
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.split takes a regular expression. Take a look at the API of java.util.regex.Pattern to see what this pattern can look like. You can read there how to get a "choice" pattern.
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First I would replace
String[] sep_list = { " ", "\n","[","]","{","}","(",")"};
with
Character[] sep_list = { ' ', '\n', '[', ']', '{', '}', '(', ')' };
or not

Then look at java.util.regex.Pattern class, it has static method called quote(String) and it returns "A literal string replacement"

Now you call it like Pattern.quote(sep_list[someIndex].toString()) and add it to your pattern. Strings in pattern are separated with [] characters
and all of them are surrounded with [] characters too. Example [[string][string]]
 
Marshal
Posts: 70305
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand all of that last posting. Are you sure about [] pairs? It is worthwhile looking through the Java™ Tutorials where there is a very good introduction to regular expressions.
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK sorry, here is complete example. This is first time when I did this too.
 
Rob Spoor
Sheriff
Posts: 22001
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mikko Kohtamäki wrote:


You don't need those [ and ] inside the loop; [abc] work just fine without having to writen [[a][b][c]]. And you don't need the inner [] for escaping because that's what the Pattern.quote will do.
 
Campbell Ritchie
Marshal
Posts: 70305
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that's what the [] meant.

Thank you.
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes no need for [] characters inside the loop. I did not realize the meaning of quote word , thanks for correcting me Rob. In my first post I did try to explain it first but failed though
 
Campbell Ritchie
Marshal
Posts: 70305
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mikko Kohtamäki wrote: . . , thanks . . .

 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic