permaculture playing cards*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String split()" Watch "String split()" New topic
Author

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

Joined: Jan 10, 2002
Posts: 60785
    
  65

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
bear,
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: 684

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
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mukesh Ranjan
Greenhorn

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,"@ $");
while(tokenizer.hasMoreTokens()){
System.out.println(tokenizer.nextToken());
}
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
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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: http://aspose.com/file-tools
 
subject: String split()
 
Similar Threads
reg split method in string
Difference between StringTokenizer and split
Using split method with String
Regex
String parsing