Get the last three chars from any string - Java

前端 未结 11 1469
情话喂你
情话喂你 2020-12-24 10:21

I\'m trying to take the last three chracters of any string and save it as another String variable. I\'m having some tough time with my thought process.

Strin         


        
11条回答
  •  一整个雨季
    2020-12-24 11:11

    I would consider right method from StringUtils class from Apache Commons Lang: http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#right(java.lang.String,%20int)

    It is safe. You will not get NullPointerException or StringIndexOutOfBoundsException.

    Example usage:

    StringUtils.right("abcdef", 3)

    You can find more examples under the above link.

提交回复
热议问题