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
Solved

java substring method

Posted on 2014-10-01
10
487 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 29
tomcat not starting 6 68
Capture logon name 13 87
troubleshoot a python script 8 21
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

860 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