This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java 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 » Beginning Java
Bookmark "String split" Watch "String split" New topic
Author

String split

sankari subash
Greenhorn

Joined: Oct 26, 2006
Posts: 2
Hi,

i have a string 1;2;3;;;4;;;
i want to split the string and put it in a list [1,2,3,,,4,,]

But while using the split method i found that the trailing blanks are eliminated.. please tell a solution for this
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41137
    
  45
You can do that with the "String.split(String regex,int limit)" method by using a negative value for the limit parameter.


Ping & DNS - my free Android networking tools app
Harish Tiruvile
Ranch Hand

Joined: Dec 01, 2005
Posts: 99
HI,

Even you can try out using replace method...it works
FYI:
str = str.replace(";", ",");


Giving up is the easiest thing in the world to do..but holding it together when everything seems like falling apart is true strength!!
with regards, Harish.T
Nalaka Gamage
Greenhorn

Joined: Nov 15, 2006
Posts: 8
public class Test1 {
public static void main(String[] args) {
String aa = "1;2;3;;;4;;;";
String bb = aa.replace(';',',');
String[] cc = bb.split(";");
java.util.List dd = Arrays.asList(cc);
System.out.println(dd);
}
}

Output:[1,2,3,,,4,,,]

Regards,
Nalaka
http://javatouch.googlepages.com
Dave Jones
Ranch Hand

Joined: Feb 20, 2005
Posts: 77
int limit = -1;
String[] cc = aa.split(";", limit);

Or limit should be the length of the reslut(slots) you want.

Read more in the API(look for the split method:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html
gaurav abbi
Ranch Hand

Joined: Jan 05, 2007
Posts: 108
hi,
i'm not able to understand the program

when
String bb = aa.replace(';',',');
this happens, there is no such pattern as ';' in the string bb

so how can we split it based on this

String[] cc = bb.split(";");

and its working fine...

any light on this.


thanks,<br />gaurav abbi
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

Originally posted by gaurav abbi:
hi,
i'm not able to understand the program

when
String bb = aa.replace(';',',');
this happens, there is no such pattern as ';' in the string bb

so how can we split it based on this

String[] cc = bb.split(";");

and its working fine...

any light on this.



Actually, it doesn't work. What is happening is that you simply changed the delimiter to a comma, which is the separator used by the List toString() method.

So you have one element that happens to have commas, and not nine elements, that is separated by commas.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String split
 
Similar Threads
Getting user input using Swing
Get the characters from String.
scann and split issue
String Tokenizing doubt
Problem with Tokenizer