473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

aString.split is returning unwanted empty element

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
5 2703
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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Willem | last post by:
Hello I am quite hopeless and of course a newbe. The situation: Sql2k / query I would like it ot break down the following string: 2004 Inventory:Ex.Plant Farm1:1st Cut:Premium:0094
4
by: Brian Glen Palicia | last post by:
My goal is to accept input from the user into a text box and then parse the data using split(). The first step is this tiny program to test the split() function. It runs in IE, but in Mozilla it...
3
by: John Salerno | last post by:
This is an example in the book I'm reading: string fullName = " Edward C Koop "; fullName = fullName.Trim(); string names = fullName.Split(' '); string firstName = names; //...
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
9
by: martinskou | last post by:
Hi, I'm looking for something like: multi_split( 'a:=b+c' , ) returning: whats the python way to achieve this, preferably without regexp? Thanks.
2
by: md | last post by:
Hi I am trying to use the Split function to remove characters like ' ', '=' and ';' from a string, but the problem is that if two delimiters are next to each other, an empty element is inserted...
1
by: mad.scientist.jr | last post by:
I am working in C# ASP.NET framework 1.1 and for some reason Regex.Split isn't working as expected. When trying to split a string, Split is returning an array with the entire string in element ...
5
by: Robert Dodier | last post by:
Hello, I'd like to split a string by commas, but only at the "top level" so to speak. An element can be a comma-less substring, or a quoted string, or a substring which looks like a function...
5
sicarie
by: sicarie | last post by:
I am attempting to parse a CSV, but am not allowed to install the CSV parsing module because of "security reasons" (what a joke), so I'm attempting to use 'split' to break up a comma-delimited file....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.