Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String split()

 
Reshma Reddy
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
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
 
Nishan Patel
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...
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java 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.
 
Mukesh Ranjan
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());
}
 
zahid zubair
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
Pie
Posts: 20669
65
Chrome Eclipse IDE Java 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic