Solved

java substring method

Posted on 2014-10-01
10
498 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 36

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
 
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 36

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Part One of the two-part Q&A series with MalwareTech.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

623 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