Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > String split method

Reply
Thread Tools

String split method

 
 
Anony!
Guest
Posts: n/a
 
      08-02-2004
Hi

Consider:

String s = "abcdefghijklmnopqrstuwxyz";

String[] result = s.split("[a-z]{3}"); //breaks string into substrings of
length 3

for (int i=0;i<result.length;i++)
System.out.println(result[i]);


expect:
abc
def
etc..

but got nothing

AAAAAA


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.732 / Virus Database: 486 - Release Date: 29/07/2004


 
Reply With Quote
 
 
 
 
Owen Jacobson
Guest
Posts: n/a
 
      08-02-2004
On Mon, 02 Aug 2004 05:47:26 +0000, Anony! wrote:

> Consider:
>
> String s = "abcdefghijklmnopqrstuwxyz";
>
> String[] result = s.split("[a-z]{3}"); //breaks string into substrings of
> length 3


What you've just said is that [a-z][a-z][a-z] is the pattern that sits
between each match.

Was there something specifically wrong with using substring(start, end) ?

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

 
Reply With Quote
 
 
 
 
Anony!
Guest
Posts: n/a
 
      08-02-2004

> On Mon, 02 Aug 2004 05:47:26 +0000, Anony! wrote:
>
> > Consider:
> >
> > String s = "abcdefghijklmnopqrstuwxyz";
> >
> > String[] result = s.split("[a-z]{3}"); //breaks string into substrings

of
> > length 3

>
> What you've just said is that [a-z][a-z][a-z] is the pattern that sits
> between each match.


What should the delimiter be if I want it to break it into substrings of
length 3. I thought the delimiter "[a-z]{3}" would be correct as it works
properly when using Pattern and Match classes.

> Was there something specifically wrong with using substring(start, end) ?


no, just not as elegant as the string split function.




---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.732 / Virus Database: 486 - Release Date: 29/07/2004


 
Reply With Quote
 
Liz
Guest
Posts: n/a
 
      08-02-2004

"Anony!" <(E-Mail Removed)> wrote in message
news:H8lPc.28495$(E-Mail Removed)...
>
> > On Mon, 02 Aug 2004 05:47:26 +0000, Anony! wrote:
> >
> > > Consider:
> > >
> > > String s = "abcdefghijklmnopqrstuwxyz";
> > >
> > > String[] result = s.split("[a-z]{3}"); //breaks string into substrings

> of
> > > length 3


Did you try escaping the curley brackets? "[a-z]\{3\}", or perhaps double
slashes.


> >
> > What you've just said is that [a-z][a-z][a-z] is the pattern that sits
> > between each match.

>
> What should the delimiter be if I want it to break it into substrings of
> length 3. I thought the delimiter "[a-z]{3}" would be correct as it works
> properly when using Pattern and Match classes.
>
> > Was there something specifically wrong with using substring(start, end)

?
>
> no, just not as elegant as the string split function.
>
>
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.732 / Virus Database: 486 - Release Date: 29/07/2004
>
>



 
Reply With Quote
 
Owen Jacobson
Guest
Posts: n/a
 
      08-02-2004
On Mon, 02 Aug 2004 06:26:47 +0000, Anony! wrote:

>
>> On Mon, 02 Aug 2004 05:47:26 +0000, Anony! wrote:
>>
>> > Consider:
>> >
>> > String s = "abcdefghijklmnopqrstuwxyz";
>> >
>> > String[] result = s.split("[a-z]{3}"); //breaks string into substrings

> of
>> > length 3

>>
>> What you've just said is that [a-z][a-z][a-z] is the pattern that sits
>> between each match.

>
> What should the delimiter be if I want it to break it into substrings of
> length 3. I thought the delimiter "[a-z]{3}" would be correct as it works
> properly when using Pattern and Match classes.


It shouldn't. Split *does not* do what you want. In all cases,
split(regex) consumes the text matching the regex. No matter what
delimiter pattern you choose some characters will be lost.

>> Was there something specifically wrong with using substring(start, end)
>> ?

>
> no, just not as elegant as the string split function.


/** Totally untested. */
private static String[] fixedSegments (String s, int length) {
assert (length > 0);

int segments = (s.length() + length - 1) / length;
if (s.length () % length != 0)
++segments; // hold the last partial segment

String[] r = new String[segments];
for (int i = 0; i < segments; ++i)
r[i] = s.substring (i * length,
Math.min ((i + 1) * length, s.length ()));

return r;
}

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

 
Reply With Quote
 
andrewh1
Guest
Posts: n/a
 
      08-02-2004


Anony! wrote:

> Hi
>
> Consider:
>
> String s = "abcdefghijklmnopqrstuwxyz";
>
> String[] result = s.split("[a-z]{3}"); //breaks string into substrings of
> length 3


No it doesn't. You need to read the docs.

The docs for 1.4

***********************************************
split

public String[] split(String regex)

Splits this string around matches of the given regular expression.

This method works as if by invoking the two-argument split method
with the given expression and a limit argument of zero. Trailing empty
strings are therefore not included in the resulting array.

The string "boo:and:foo", for example, yields the following results
with these expressions:

Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }

Parameters:
regex - the delimiting regular expression
Returns:
the array of strings computed by splitting this string around
matches of the given regular expression
************************************************** ******************
Effectively you are getting it to give you the strings separated by (and
not including) any lower case letter in the alphabet. In your case lots
of "".
However since it 'doesn't include trailing empty strings' it should
return an empty array.

I may be wrong, but I doubt using regular expressions is the way to go
if you simply want a string chopped into equal sized fragments
disregarding character content. String.Substring() method is probably
the best.

Seems to me that regular expressions are more for dealing with strings
where string content (ie the character sequence) defines the operations.

Cheers

Andrew

>
> for (int i=0;i<result.length;i++)
> System.out.println(result[i]);
>
>
> expect:
> abc
> def
> etc..
>
> but got nothing
>
> AAAAAA
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.732 / Virus Database: 486 - Release Date: 29/07/2004
>
>


 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      08-02-2004
Anony! wrote:

> > Was there something specifically wrong with using substring(start, end)
> > ?

>
> no, just not as elegant as the string split function.


Elegance is in the eye of the beholder. To /my/ eye, using regexps when no
regexp is needed looks like unecessary bloat, obfuscation, and the misuse of
heavyweight (and slow[*]) features to perform lightweight operations.

(Not to say that it is /necessarily/ wrong, though, e.g. if I had lots of
string splitting to do, nearly all of which needed full regexp power, then I
would quite probably use it for the simple cases too. Just to keep things
simple and regular.)
[*] I admit I haven't measured the difference.

-- chris


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use String.split to split a mixed encoding string(partencoded in gbk, part encoded in utf-8) Stanley Xu Ruby 2 03-23-2011 02:06 PM
a split is not a split Dumbell Computer Support 3 03-09-2009 10:45 PM
String#split(/\s+/) vs. String#split(/(\s+)/) Sam Kong Ruby 5 08-12-2006 07:59 PM
How can I split database results with ExecuteReader and Split? needin4mation@gmail.com ASP .Net 2 05-05-2006 10:36 PM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM



Advertisments