Results 1 to 4 of 4
Thread: problem in spliting a text
- 01-10-2011, 09:21 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 1
- Rep Power
- 0
problem in spliting a text
Hi,
I am using "\\s+" to seperate string on behalf of multiple space. But when the space occur at the starting of string in that case it took that as a token. Plz tell me the reason behind it. While the same thing works in StringTokenizer
String input=" one two three four five ";
//using split method
String[] outputArr=input.split("\\s+");
for(int i=0;i<outputArr.length;i++)
System.out.println("Element["+i+"]:"+outputArr[i]);
//using tokenizer
StringTokenizer str=new StringTokenizer(input);
while(str.hasMoreElements())
System.out.println("Element:"+str.nextToken());
- 01-10-2011, 09:38 AM #2
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
you will have to read the StringTokenizer API to see how it works (see especially the part where it explains what a token represents). one way to solve your problem if you don't want the extra space in front is to do a trim() first before splitting.
- 01-10-2011, 09:48 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-10-2011, 10:09 PM #4
Similar Threads
-
Spliting a string (45 Characters)
By BobAmin in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 11-20-2010, 09:45 AM -
spliting a string and checking each token's format
By Implode in forum New To JavaReplies: 1Last Post: 10-18-2009, 08:41 PM -
Spliting the Panel to three panels
By suraw in forum New To JavaReplies: 0Last Post: 03-25-2009, 06:05 PM -
Problem With Text Fields!
By freshoreo in forum AWT / SwingReplies: 3Last Post: 08-04-2008, 09:52 PM -
Spliting desktop
By nagarte in forum AWT / SwingReplies: 5Last Post: 05-05-2008, 12:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks