Solved

java substring method

Posted on 2014-10-01
10
471 Views
Last Modified: 2014-10-02
Hi,

I was going through below link

http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#substring-int-

I have not understood why it returns empty string. What it means by empty sting i see
e-1(looks like index starting from1 not 0)
m-2
--
s-8
s-9
after 9 position there is no other letter

"emptiness".substring(9) returns "" (an empty string)

Please advise
0
Comment
Question by:gudii9
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 14

Expert Comment

by:CPColin
ID: 40355676
What do you think it should return?
0
 
LVL 16

Assisted Solution

by:krakatoa
krakatoa earned 100 total points
ID: 40355764
We are back to this question about indexing once again, which we have covered often.

Indexes start at 0. So 9 in this case, is one position longer than the string you are looking at INDEX-wise. There are 9 characters in the string, but the ninth is at position 8.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40356168
i am not clear on

"emptiness".substring(9) returns "" (an empty string)

why above one returns empty string
0
 
LVL 35

Accepted Solution

by:
mccarl earned 300 total points
ID: 40356180
Indexes start at 0, and substring(int) will return the string starting from (and including) the index passed in, and all the characters until the end of the string, so lets work this one through...

"emptiness".substring(0)    ==   "emptiness"              // index is 0, which points to the first character of the string "e" so that character is included and all the others after it...

"emptiness".substring(1)    ==   "mptiness"                // index is 1 which is the "m" in the string, etc, etc...
"emptiness".substring(2)    ==   "ptiness"
"emptiness".substring(3)    ==   "tiness"
"emptiness".substring(4)    ==   "iness"
"emptiness".substring(5)    ==   "ness"
"emptiness".substring(6)    ==   "ess"
"emptiness".substring(7)    ==   "ss"
"emptiness".substring(8)    ==   "s"
"emptiness".substring(9)    ==   ""
0
 
LVL 7

Author Comment

by:gudii9
ID: 40358244
does not it supposed to return null.
how null is different from empty string""
Please advise
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 100 total points
ID: 40358253
Empty strings are non-null.

For example, this code works fine:

String emptyString = "";

System.out.println(emptyString.length());

Open in new window


This code throws a NullPointerException:

String nullString = null;

System.out.println(nullString.length());

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40358264
i see
      System.out.println("actual string "+str+" reversed string "+reversedStr);
      String emptyString = "";

      System.out.println("emptyString.length is--->"+emptyString.length());
emptyString.length is --->0 which makes sense.

After emptiness(after last 's') there is nothing is there right(i mean no other character/characters is there right) in that case why it returns "" for below
"emptiness".substring(9)    ==   ""

please advise
0
 
LVL 14

Expert Comment

by:CPColin
ID: 40358275
Yes.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40358561
After emptiness(after last 's') there is nothing is there right(i mean no other character/characters is there right) in that case why it returns "" for below
"emptiness".substring(9)    ==   ""

I feel it should throw exception since we are trying substring on some thing that is not existing instead of giving back "". please advise
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40358574
I feel it should throw exception since ...
That's nice, but the person/people who designed the Java language obviously felt differently to you... That's just how it is!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now