• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String split

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do that with the "String.split(String regex,int limit)" method by using a negative value for the limit parameter.
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

Even you can try out using replace method...it works
FYI:
str = str.replace(";", ",");
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic