Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

java substring method

Posted on 2014-10-01
10
Medium Priority
?
505 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 17

Assisted Solution

by:krakatoa
krakatoa earned 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 36

Accepted Solution

by:
mccarl earned 1200 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 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to implement Singleton Design Pattern in Java.
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.
Suggested Courses

824 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