开发者

Get right part of string in Android app

开发者 https://www.devze.com 2023-01-17 22:51 出处:网络
Ok, I am trying to get the right three characters of a string in an Android app I\'m writing. Based on the research I\'ve done in the SDK and online I should be able to use something like the followi

Ok, I am trying to get the right three characters of a string in an Android app I'm writing.

Based on the research I've done in the SDK and online I should be able to use something like the following to do it.

millisecondsD = millisecondsD.substring(millisecondsD.length() -3, millisecondsD.length());

However, I get a force close whenever I try to use to use t开发者_如何学Pythonhe code above. Am I just missing something super simple?


You may need to check to see that the string is at least 3 characters long and not null first.

if (millesecondsD != null && millisecondsD.length() >=3)
    millesecondsD = milisecondsD.substring(milisecondsD.length() - 3);

Also, you can leave off the second parameter if you want.

Example:

String:
I'm a chunky monkey from funky town
01234567890123456789012345678901234
         10        20        30

So, in this case the last 3 chars are 34, 33, and 32. The indices you have in your code are correct.

0

精彩评论

暂无评论...
验证码 换一张
取 消