Reputation: 35
I have a string that end with a space like this :
String s="somthing= "
I want to get that space so I do :
s.substring(s.indexOf("=")+1).trim()
but it doesn't work. Any idea?
Upvotes: 0
Views: 248
Reputation: 10055
You're not modifying your string s
, just the substring. The specs say that substring()
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.
The trim()
method affects the substring, not s
, so that's why it doesn't work. By doing s.trim()
directly you'll remove the whitespaces at the end (and beginning) of s
, but watch out since this method also returns a new ("a copy" says the specs) String
, so try doing s = s.trim();
.
Upvotes: 5