Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the last three chars from any string - Java

Tags:

java

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.

String word = "onetwotwoone"
int length = word.length();
String new_word = id.getChars(length-3, length, buffer, index);

I don't know how to use the getChars method when it comes to buffer or index. Eclipse is making me have those in there. Any suggestions?

like image 938
EGHDK Avatar asked Mar 06 '13 16:03

EGHDK


People also ask

How do I get the last 3 characters of a string in Java?

To access the last n characters of a string in Java, we can use the built-in substring() method by passing String. length()-n as an argument to it. The String. length() method returns the total number of characters in a string, n is the number of characters we need to get from a string.

How do I print the last 3 characters of a string?

string str = "AM0122200204"; string substr = str. Substring(str. Length - 3);

How do you get the last few characters of a string?

To get the last N characters of a string, call the slice method on the string, passing in -n as a parameter, e.g. str. slice(-3) returns a new string containing the last 3 characters of the original string. Copied!


3 Answers

Why not just String substr = word.substring(word.length() - 3)?

Update

Please make sure you check that the String is at least 3 characters long before calling substring():

if (word.length() == 3) {
  return word;
} else if (word.length() > 3) {
  return word.substring(word.length() - 3);
} else {
  // whatever is appropriate in this case
  throw new IllegalArgumentException("word has fewer than 3 characters!");
}
like image 184
Egor Avatar answered Oct 21 '22 07:10

Egor


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.

like image 38
Michal Przysucha Avatar answered Oct 21 '22 08:10

Michal Przysucha


Here's some terse code that does the job using regex:

String last3 = str.replaceAll(".*?(.?.?.?)?$", "$1");

This code returns up to 3; if there are less than 3 it just returns the string.

This is how to do it safely without regex in one line:

String last3 = str == null || str.length() < 3 ? 
    str : str.substring(str.length() - 3);

By "safely", I mean without throwing an exception if the string is nulls or shorter than 3 characters (all the other answers are not "safe").


The above code is identical in effect to this code, if you prefer a more verbose, but potentially easier-to-read form:

String last3;
if (str == null || str.length() < 3) {
    last3 = str;
} else {
    last3 = str.substring(str.length() - 3);
}
like image 31
Bohemian Avatar answered Oct 21 '22 06:10

Bohemian