By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,694 Members | 1,846 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,694 IT Pros & Developers. It's quick & easy.

aString.split is returning unwanted empty element

P: n/a
Good morning.

I want to send split to a String such as " abc def " and receive
back an Array with the two elements String(abc) and String(def); and I want
to receive back a zero-length Array if the target is empty or contains only
white space.

The following code produces an unwanted empty string as the first element.
This also occurs when the target is a zero-length String or a String
composed only of spaces.

After all morning, I can't find the solution. Thanks for any help, Ron.

" abc def ".split("\\s+")
=> Array("", "abc", "def")
" ".split("\\s+")
=> Array("")

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Fri, 22 Aug 2003 12:55:29 -0400, "Ron Brennan" <rb******@magma.ca>
wrote:
Good morning.

I want to send split to a String such as " abc def " and receive
back an Array with the two elements String(abc) and String(def); and I want
to receive back a zero-length Array if the target is empty or contains only
white space.


Have you looked at the StringTokenizer class?
Jul 17 '05 #2

P: n/a
Yes, I've looked at it; but it's been deprecated in favor of split.
"Bryce (Work)" <sp******@berzerker-soft.com> wrote in message
news:fe********************************@4ax.com...
On Fri, 22 Aug 2003 12:55:29 -0400, "Ron Brennan" <rb******@magma.ca>
wrote:
Good morning.

I want to send split to a String such as " abc def " and receive
back an Array with the two elements String(abc) and String(def); and I wantto receive back a zero-length Array if the target is empty or contains onlywhite space.


Have you looked at the StringTokenizer class?

Jul 17 '05 #3

P: n/a
On Fri, 22 Aug 2003 16:43:20 -0400, "Ron Brennan" <rb******@magma.ca>
two-finger typed:
Yes, I've looked at it; but it's been deprecated in favor of split.
Why don't you trim(), before you split() ?

"Bryce (Work)" <sp******@berzerker-soft.com> wrote in message
news:fe********************************@4ax.com.. .
On Fri, 22 Aug 2003 12:55:29 -0400, "Ron Brennan" <rb******@magma.ca>
wrote:
>Good morning.
>
>I want to send split to a String such as " abc def " and receive
>back an Array with the two elements String(abc) and String(def); and Iwant >to receive back a zero-length Array if the target is empty or containsonly >white space.


Have you looked at the StringTokenizer class?


Cheers.
Jul 17 '05 #4

P: n/a

"Neomorph" <ne******@nospam.demon.co.uk> wrote in message
news:f6********************************@4ax.com...
On Fri, 22 Aug 2003 16:43:20 -0400, "Ron Brennan" <rb******@magma.ca>
two-finger typed:
Yes, I've looked at it; but it's been deprecated in favor of split.
Why don't you trim(), before you split() ?

"Bryce (Work)" <sp******@berzerker-soft.com> wrote in message
news:fe********************************@4ax.com.. .
On Fri, 22 Aug 2003 12:55:29 -0400, "Ron Brennan" <rb******@magma.ca>
wrote:
>Good morning.
>
>I want to send split to a String such as " abc def " and receive >back an Array with the two elements String(abc) and String(def); and Iwant >to receive back a zero-length Array if the target is empty or containsonly >white space.


Have you looked at the StringTokenizer class?


Cheers.

Yes, I did, and it worked.

Thank you.

Ron.

Jul 17 '05 #5

P: n/a
On Mon, 25 Aug 2003 22:09:06 +0200, Neomorph
<ne******@nospam.demon.co.uk> wrote:
On Mon, 25 Aug 2003 10:54:59 -0400, "Bryce (Work)"
<sp******@berzerker-soft.com> two-finger typed:
On Fri, 22 Aug 2003 16:43:20 -0400, "Ron Brennan" <rb******@magma.ca>
wrote:
Yes, I've looked at it; but it's been deprecated in favor of split.


Hey... I learned something new today. I hadn't realized it had been
deprecated...


StringTokenizer is not listed in the deprecated API for the 1.4.2 version
of the JSDK.


I know.. But if you read the API for 1.4.2, it says:

http://java.sun.com/j2se/1.4.2/docs/...Tokenizer.html

StringTokenizer is a legacy class that is retained for compatibility
reasons although its use is discouraged in new code. It is recommended
that anyone seeking this functionality use the split method of String
or the java.util.regex package instead.

--- END QUOTE --

Note, that this does not appear in the 1.4.1 docs. Maybe its just prep
for them deprecating in 1.5...
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.