Fcoder
Fcoder

Reputation: 9216

get a character from a string

I have this string:

String code="abc";

i want to extract "c" from this string by substring method but it doesn't work:

String code="abc";
String getsmscode=code.substring(2,1);

This code return an error

java.lang.StringIndexOutOfBoundsException: length=3; regionStart=2; regionLength=-1

but i don't know why?

Upvotes: 2

Views: 674

Answers (6)

Ajay S
Ajay S

Reputation: 48592

Use can achieve this like also.

String getsmscode = code.substring(2,3);

Method Description

public String substring (int start, int end)

Returns a string containing a subsequence of characters from this string. The returned string shares this string's backing array.

Parameters

start - the offset of the first character.

end- the offset one past the last character.

Returns a new string containing the characters from start to end - 1

Throws

IndexOutOfBoundsException   if start < 0, start > end or end > length(). 

Upvotes: 0

Ali
Ali

Reputation: 167

The guys above have answered your question adequately but just a few tips on reading the API.

Methods usually start with the word public and then include the return type... that is what type of data will be returned. If nothing is expected to be returned is void.

Then comes the name of the method and two parenthesis.

In between the parenthesis are the parameter type it accepts.

with this particular method it accepts two integers. these help keep an index of how much of a string you'd like to keep

index is an important concept to understand and it's hard to describe how it all works in words but looking at the examples in the api description listings help.

index-1 usually means counting starts at 0 so in the String "Hello World" H is indexed 0 and d is index 10. Notice there are only 9 letters but we still must include the space as a character so it's index in this case is 5.

it's important to note the api mentions excludes and includes in the parameter descriptions. This just means the returned string will include the letter indexed by the first parameter but will not include the letter indexed by the second parameter.

if all you need to do is extract the last letter in a string, no matter how long the string size, a simple solution is

stringName.substring(stringName.length()-1); 

Upvotes: 3

confused_at_times
confused_at_times

Reputation: 545

I think substring takes a start index or a start index and end index as parameters:

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

You've given it a start index greater than the end index, hence it is getting confused as the length returned is then negative. Swap the 1 and 2 around.

Upvotes: 0

1218985
1218985

Reputation: 8012

The index start from 0. You can try

String code="abc";
String getsmscode=code.substring(2);

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499860

You need to read the documentation for substring - the second parameter isn't a length, it's the end index (exclusive).

Parameters:
beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.

When in doubt, read the docs...

Also note that if you're just trying to get the final character, you can just use the single-parameter overload, which returns a substring from a given start point to the end of the string:

String lastCharacter = text.substring(text.length() - 1);

Or you could get it as a single character:

char lastCharacter = text.charAt(text.length() - 1);

Upvotes: 14

bsiamionau
bsiamionau

Reputation: 8229

Second parameter is not a length, it is an end index.

  String#substring(int beginIndex, int endIndex) 

Upvotes: 7

Related Questions