• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String.split() method functionality

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The below mentioned source is a self created one.

The output is
Number of tokens generated: 5 // line 1

1 // line 3
2
34
56789


My question is regarding the blank line between line 1 and line 3.
Can anyone explain me why it is printed?
 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajshekhar Paul wrote:The below mentioned source is a self created one.

The output is
Number of tokens generated: 5 // line 1

1 // line 3
2
34
56789


My question is regarding the blank line between line 1 and line 3.
Can anyone explain me why it is printed?



I believe you're confusing the first character of each line with your output:
"a1a2a34a56789a" with a token of "a"

<= first line is a space because of "a" being the first character
1 <= line 2
2 < = line 3
34 <= line 4
56789 <= line 5
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul for the reply.

But when you say

Paul Campbell wrote: <= first line is a space because of "a" being the first character


Is it like when the first character is same as the delimiter, the first token will always be a blank line? And if it is, can you tell me the logic behind that?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the first token is not a space or a blank line, it is the empty string. What happens is that split() considers a token to be a substring of the input that either ends in a separator, or at the end of the string. Your first token starts at the beginning of the string, but since the first character of the input is a separator, the token will be input.substring(0,0) (which is "")
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ruben for the explanation.
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But, If I change the input String to
a1a2a34a56789aaa, still the noof tokens generated is 5. Why so??
 
Paul Campbell
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:Actually the first token is not a space or a blank line, it is the empty string. What happens is that split() considers a token to be a substring of the input that either ends in a separator, or at the end of the string. Your first token starts at the beginning of the string, but since the first character of the input is a separator, the token will be input.substring(0,0) (which is "")



You're right Ruben. I misstated it.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Rajshekhar Paul: My question is regarding the blank line between line 1 and line 3.
Can anyone explain me why it is printed?


The line appeared because of System.out.println inserted a line seperator. Try System.out.print, there is no line
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Vinayak
I don't think its the reason. Because if you run the below code, you will not find any empty string though it contains println statements.


Output is
Number of tokens generated: 4
1
2
34
56789
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhi vijay wrote:But, If I change the input String to
a1a2a34a56789aaa, still the noof tokens generated is 5. Why so??


What's the explanation to this?
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Adat wrote:

Abhi vijay wrote:But, If I change the input String to
a1a2a34a56789aaa, still the noof tokens generated is 5. Why so??


What's the explanation to this?



Hi Sachin,

Actually public String[] split(String regex) invokes the overloaded method public String[] split(String regex,
int limit)[/b] with limit 0.

If the limit is zero then the regex pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.


Yuan
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just try this as input

a1a2a34a567aaa89

Then the ouput will be 8 as it will create empty tokens. But if aaa is at the end, then the empty tokens are discarded. This is from the API documentation of split method

Trailing empty strings are therefore not included in the resulting array.



[Oops! Beaten to the reason by Yuan by 55 seconds]
 
Vinayak Bhat
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajshekhar Paul wrote:My question is regarding the blank line between line 1 and line 3.
Can anyone explain me why it is printed?



Raj, your question in the first post was why was there a blank line. If you use System.out.print, you would see there is no blank line.

So that clearly explains why is there a blank line, which is the answer to question you posted in the first post

Now, when you use enhanced for loop, It takes the string array that you pass to it and iterates through the individual values, which are tokens generated when "a1a2a34a56789a" was split using method of class String i.e String.split(String regex) (Check API). Note the term regex given in the String.split(regex) is an expression used as delimiter to break the String into tokens and store them in String array which is what the return type of String.split method is.

So that concludes String.split method has nothing to do with printing the blank line in between line 1 and line 3.

So, in the enhanced for loop, you are printing out the tokens, again you are using System.out.println hence printing on new line everytime.

Note the String in your first post was "a1a2a34a56789a" and now "1a2a34a56789a". It's simple, mate. No rocket science.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vinayak you could be right, but it is not as simple as you are taking, just run this code.

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



System.out.print(sa[i]+"\n");
It is this line which is printing a blank space than a "\n" at line 2.




So

System.out.print("Number of tokens generated:" + sa.length+"\n");
this line has nothing to do with blank line 2.


 
Vinayak Bhat
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think i'll go through the String chapter once again. These trivial things costing marks in exam is not good.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just go through split() part, but you will get actual learning by practicals, mocks and discussion from javaranch. Here one thing you would have learned that "a" regex is spliting and counting starting blank space, inner blank spaces but avoiding trailing blank spaces.
 
Vinayak Bhat
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Singh wrote:Just go through split() part, but you will get actual learning by practicals, mocks and discussion from javaranch. Here one thing you would have learned that "a" regex is spliting and counting starting blank space, inner blank spaces but avoiding trailing blank spaces.



Punit, You are right. Trailing empty strings are not included in the resulting array. http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#split(java.lang.String).
 
Vinayak Bhat
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Singh wrote:Just go through split() part, but you will get actual learning by practicals, mocks and discussion from javaranch. Here one thing you would have learned that "a" regex is spliting and counting starting blank space, inner blank spaces but avoiding trailing blank spaces.



Punit, You are right. Trailing empty strings are not included in the resulting array. http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#split(java.lang.String).
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all !!!

Before I started coming to the ranch regularly, I was thinking of giving the exam last year.....
Everyday I come at the ranch, I postpone my date of giving the exam...........
Now its postponed indefinitely(I can wait till June)........
Not just because I'm afraid I'll fail, but also everyday I feel I've learnt something more ........

I've finished K&B 6, though I guess I need atleast 2 more revisions..............
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohhh, just go for exam, you can learn after your exams also, as we are learning here.

Your learning and knowledge matters more than your exam %.
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Singh wrote:Ohhh, just go for exam, you can learn after your exams also, as we are learning here.
Your learning and knowledge matters more than your exam %.


No way....... I got enough time, so don't want to rush into it.........

Punit Singh wrote:Your learning and knowledge matters more than your exam %.


Agree, so does the exam fee........
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great discussion guys!

@Sachin
Same thing is also happening with me. Each day, I find a new question to discuss and think I haven't learnt enough to sit for the exam!
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sachin I would not recommend you to wait for so long. It actually depends on your circumstances. As you say that you have enough time, then it's OK. But I know that you will give the exam before June. Because I have experience with this. I also was waiting and waiting for the exam. And the day on which I decided that I was ready, I didn't get a voucher and had to wait for extra 1.5 months. And after a certain amount of preparation, you start feeling that SCJP is pushing you back and you have got other things to move on like other certifications or practical studies like Struts etc. So if you feel that the time you want to give SCJP is worth it, then you can wait. But don't waste too much time on it as there are a lot of other things to learn...
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:And the day on which I decided that I was ready, I didn't get a voucher and had to wait for extra 1.5 months.


Oops!!! That was harsh.........
Anyway I've bought my voucher and I am not planning to wait till June.........no way, its just TIME_MAX.

Ankit Garg wrote:And after a certain amount of preparation, you start feeling that SCJP is pushing you back and you have got other things to move on like other certifications or practical studies like Struts etc. So if you feel that the time you want to give SCJP is worth it, then you can wait. But don't waste too much time on it as there are a lot of other things to learn...


I guess you and Punit are right, actually I bought the Head First book for SCWCD before I got K&B SCJP 6.......

Will try to give it by Feb end..........now that's not a promise.......
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sachin Adat wrote:
Anyway I've bought my voucher and I am not planning to wait till June.........no way, its just TIME_MAX.



Then just call isReadyState() and check if it returns true and execute the giveSCJP() method when isReadyState() returns true

(Now that smells Geeky)
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having the same problem as you, Sachin. But I am going to schedule very soon, because Punit and Ankit are right. You can never learn everything, so there will always be things you don't know. And if you spend too long on the exam then you can become bored and frustrated, as the number of new things you will learn will decrease overtime. It's the law of diminishing returns.
Good luck with it though, I know that it's tough making up your mind.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I was following this discussion and I quite could not get how you can get 8 tokens when the input is a1a2a34a567aaa89 and the regex used is "a" .

I got the following output:
count 8
><
>1<
>2<
>34<
>567<
><
><
>89<
I don't understand why there are 2 empty strings here. Please help.
Thanks,
Meera
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Meera the two empty strings come from here (Ignore the spaces between the As)



Since we are searching for a, and consecutive As were found, so split method created empty tokens...
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The split() method uses substring method of String class.Therefore,for a1a2a34a567aaa89 the tokens generated will be:

a1a2a34a567aaa89.substring(0,0) - ""
a1a2a34a567aaa89.substring(1,2) - 1
a1a2a34a567aaa89.substring(3,4) - 2
a1a2a34a567aaa89.substring(5,7) - 34
a1a2a34a567aaa89.substring(8,11) - 567
a1a2a34a567aaa89.substring(12,12) - ""
a1a2a34a567aaa89.substring(13,13) - ""
a1a2a34a567aaa89.substring(14,16) - 89

 
reply
    Bookmark Topic Watch Topic
  • New Topic