How do I get the last character of a string?

前端 未结 11 2183
遥遥无期
遥遥无期 2020-12-02 07:59

How do I get the last character of a string?

public class Main {
    public static void main(String[] args)  {
        String s = "test string";
            


        
相关标签:
11条回答
  • 2020-12-02 08:50
    String aString = "This will return the letter t";
    System.out.println(aString.charAt(aString.length() - 1));
    

    Output should be:

    t
    

    Happy coding!

    0 讨论(0)
  • 2020-12-02 08:51

    The other answers contain a lot of needless text and code. Here are two ways to get the last character of a String:

    char

    char lastChar = myString.charAt(myString.length() - 1);
    

    String

    String lastChar = myString.substring(myString.length() - 1);
    
    0 讨论(0)
  • 2020-12-02 08:55

    Simple solution is:

    public String frontBack(String str) {
      if (str == null || str.length() == 0) {
        return str;
      }
      char[] cs = str.toCharArray();
      char first = cs[0];
      cs[0] = cs[cs.length -1];
      cs[cs.length -1] = first;
      return new String(cs);
    }
    

    Using a character array (watch out for the nasty empty String or null String argument!)

    Another solution uses StringBuilder (which is usually used to do String manupilation since String itself is immutable.

    public String frontBack(String str) {
      if (str == null || str.length() == 0) {
        return str;
      }
      StringBuilder sb = new StringBuilder(str);  
      char first = sb.charAt(0);
      sb.setCharAt(0, sb.charAt(sb.length()-1));
      sb.setCharAt(sb.length()-1, first);
      return sb.toString();
    }
    

    Yet another approach (more for instruction than actual use) is this one:

    public String frontBack(String str) {
      if (str == null || str.length() < 2) {
        return str;
      }
      StringBuilder sb = new StringBuilder(str);
      String sub = sb.substring(1, sb.length() -1);
      return sb.reverse().replace(1, sb.length() -1, sub).toString();
    }
    

    Here the complete string is reversed and then the part that should not be reversed is replaced with the substring. ;)

    0 讨论(0)
  • 2020-12-02 08:56

    The code:

    public class Test {
        public static void main(String args[]) {
            String string = args[0];
            System.out.println("last character: " +
                               string.substring(string.length() - 1)); 
        }
    }
    

    The output of java Test abcdef:

    last character: f
    
    0 讨论(0)
  • 2020-12-02 08:57

    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) return "";
        if (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)
提交回复
热议问题