Get the last three chars from any string - Java

前端 未结 11 1423
情话喂你
情话喂你 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 10:56

    Alternative way for "insufficient string length or null" save:

    String numbers = defaultValue();
    try{
       numbers = word.substring(word.length() - 3);
    } catch(Exception e) {
       System.out.println("Insufficient String length");
    }
    
    0 讨论(0)
  • 2020-12-24 10:56

    Here is a method I use to get the last xx of a string:

    public static String takeLast(String value, int count) {
        if (value == null || value.trim().length() == 0 || count < 1) {
            return "";
        }
    
        if (value.length() > count) {
            return value.substring(value.length() - count);
        } else {
            return value;
        }
    }
    

    Then use it like so:

    String testStr = "this is a test string";
    String last1 = takeLast(testStr, 1); //Output: g
    String last4 = takeLast(testStr, 4); //Output: ring
    
    0 讨论(0)
  • 2020-12-24 10:57

    You can use a substring

    String word = "onetwotwoone"
    int lenght = word.length(); //Note this should be function.
    String numbers = word.substring(word.length() - 3);
    
    0 讨论(0)
  • 2020-12-24 11:04

    This method would be helpful :

    String rightPart(String text,int length)
    {
        if (text.length()<length) return text;
        String raw = "";
        for (int i = 1; i <= length; i++) {
            raw += text.toCharArray()[text.length()-i];
        }
        return new StringBuilder(raw).reverse().toString();
    }
    
    0 讨论(0)
  • 2020-12-24 11:07

    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);
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题