Meaningless Drivel is fun!
The moose likes Java in General and the fly likes String split() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String split()" Watch "String split()" New topic

String split()

Reshma Reddy
Ranch Hand

Joined: Dec 21, 2006
Posts: 197
Hi All,
I want to split string into diff parts

Expected out put is: welcome

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

Joined: Jan 10, 2002
Posts: 63349

What did you find when you perused the String javadoc?

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Reshma Reddy
Ranch Hand

Joined: Dec 21, 2006
Posts: 197
I couldn't get any thing about my requirement, give me any clue to slove this
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 689

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...

Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Rob Spoor

Joined: Oct 27, 2005
Posts: 20132

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.

How To Ask Questions How To Answer Questions
Mukesh Ranjan

Joined: Jun 24, 2009
Posts: 22

StringTokenizer will help you.
Below is the code:
String name = "welcome@ to$ java world!";
StringTokenizer tokenizer = new StringTokenizer(name,"@ $");
zahid zubair
Ranch Hand

Joined: Aug 29, 2009
Posts: 32
Try this:
Reshma Reddy
Ranch Hand

Joined: Dec 21, 2006
Posts: 197
thanks zhid,

it is working perfect..
Rob Spoor

Joined: Oct 27, 2005
Posts: 20132

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.
I agree. Here's the link:
subject: String split()
jQuery in Action, 3rd edition