Solved

java substring method

Posted on 2014-10-01
10
494 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
Independent Software Vendors: 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 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
 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Link failure 16 91
junit example issue 2 56
IntelliJ and Eclipse Neon 2 installations 4 66
Hibernate Inheritance Strategy for Abstract Base class and two concrete classes 3 38
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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