Results 1 to 8 of 8
Thread: problem with split method
- 01-17-2008, 05:17 AM #1
Member
- Join Date
- Jan 2008
- Posts
- 23
- Rep Power
- 0
problem with split method
Hi
i m using split methos and want to display string elements seperatly..
String quoteValues = ";;12";
String[] quote = quoteValues.split(";");
for(int i=1 ; i<quote.length ; i++)
{
if(quote[i] != "")
System.out.println(" quote--"+quote[i]);
}
but i m getting output--
quote--
quote--12
i want only to get--
quote--12
how to get..
plz reply..
- 01-17-2008, 05:39 AM #2
Pointer: ==, is String a native type ??Java Code:if(!quote[i].equals(""))Last edited by roots; 01-17-2008 at 05:41 AM.
dont worry newbie, we got you covered.
- 01-17-2008, 05:45 AM #3
The split method recognizes your token, in this case ';' and prints out the strings before or after it, if they exist - but not including the token. So after reading the first token, it looks to see surrounding strings, and finds ";12" - thus it finds another token which it can't print resulting in the undesired output. From there, I assume you know how split() operates. The pattern you're looking for is to exclude empty strings, thus:
Hope this helps. :)Java Code:String[] quote = quoteValues.split(";+");Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 01-17-2008, 07:31 AM #4
Member
- Join Date
- Jan 2008
- Posts
- 23
- Rep Power
- 0
thanks..it works.. my simple mistake..
- 02-09-2009, 01:27 PM #5
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
Facing problem with some charecters
I have used the split API with "|" and i am not getting the proper result.
Can somebody tell me why..
the sample code is given under
public static void main(String[] args) {
String a = "a|c|d|e";
String b[] = a.split("|");
for(int i=0; i<b.length; i++){
System.out.print(b[i]+" ");
}
System.out.println(b.length);
}
and the result i got is:
a | c | d | e 8
any idea abt this?
- 02-09-2009, 02:11 PM #6
Meta character
Your problem is that "|" is a regex metacharacter:
Try replacing "|" with "\\|"The metacharacters supported by this API are: ([{\^-$|]})?*+.
Here's a good guide for regex:
Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
Luck,
CJSL
PS: Please open your own post... don't post on other old posts.Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-10-2009, 11:47 AM #7
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
it worked
Thanks.... It worked after using the "\\|" in split
- 02-10-2009, 01:54 PM #8
Similar Threads
-
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 08:27 PM -
how to split a file
By nagaraaju in forum New To JavaReplies: 0Last Post: 03-14-2008, 08:45 AM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:32 PM -
Problem with sort method
By Albert in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 07:12 PM -
PDF Split and Merge 0.7 beta 1
By JavaBean in forum Java SoftwareReplies: 0Last Post: 06-24-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks