• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

String.split() - Trailing empty strings

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends, I am doing some excercises, and I can not undertand this:


Output:

[]
[, , , , XXX, , XXX]

I saw this link: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String), but I do not understand exactly what mean "Trailing empty strings".
For the line 03 I thought that will be show the output: [XXX, , XXX].

I thought that all empty result in the end and start will be delete, but only are delete the emply result in the end!

What is my mistake?

Thanks!
 
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alam,

The API documentation of String method spilt() states:

Java API documentation wrote:public String[] split(String regex)
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.


So when you call "aaaaXXXaaXXXaaaa".split("a"), the resultant array excludes the trailing empty Strings. It doesn't exclude the leading empty String values.

This explains why "aaaaaaaaaaaaaaaa".split("a") returns [].

With respect,
Mala
 
Alam Ameghino
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahhhhhh, ok! I got it! A lot of thanks! You explication help me a lot! The "key" was the word "leading".

I have a serious problem with english! I looked for "trailing" word, but I could not find the correct meaning.

In a few days I am going to take the real exam, and I chose the enlgish language. I hope that I will not have this problem on the real exam!

In short,

==> "trailing": end of the string?
==> "leading" : start of the string?

I can not find this meaning with the sense that I like in the dictionary! Please, let me know if I am right about the sense that I give to this words.

http://www.macmillandictionary.com/dictionary/british/trailing
http://www.wordreference.com/definition/trailing

Again, thanks!
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic