• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

String split()  RSS feed

 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I want to split string into diff parts



Expected out put is: welcome
to
java
world

I want to eleminate all special character in my string, where as in split we can mention any one character to check in string. how can I do this.
 
Sheriff
Posts: 67259
170
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you find when you perused the String javadoc?
 
Reshma Reddy
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bear,
I couldn't get any thing about my requirement, give me any clue to slove this
 
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vardhan,

use regex pattern.

Make one pattern which contains your special character. If your string contains this character split it.

May be Regix pattern will help you...
 
Sheriff
Posts: 21745
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why specify your own special characters? There are special character classes that represent all actual letters or punctuation characters; check out the API of java.util.regex.Pattern for more info.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


StringTokenizer will help you.
Below is the code:
String name = "welcome@ to$ java world!";
StringTokenizer tokenizer = new StringTokenizer(name,"@ $");
while(tokenizer.hasMoreTokens()){
System.out.println(tokenizer.nextToken());
}

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:
 
Reshma Reddy
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks zhid,

it is working perfect..
 
Rob Spoor
Sheriff
Posts: 21745
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd prefer the character class [\s\W]. \s means whitespace ([ \t\n\x0B\f\r]), whereas \W is a synonym for [^\w] and \w is a synonym for [a-zA-Z_0-9]. Of course you can't use * since then all empty strings between all characters will match as well; the regex will be [\s\W]+. And because \W implies \s, \W+ will suffice.

Of course, once you start using other word characters like é you will run into problems, since that also matches \W.
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!