• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 43028
76
  • 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: 23928
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
 
Do Re Mi Fa So La Tiny Ad
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic