如何获得字符串的最后一个字符?

时间:2023-02-02 21:37:25

How do I get the last character of a string?

如何获得字符串的最后一个字符?

public class Main
{
    public static void main(String[] args) 
    {
        String s = "test string";
        //char lastChar = ???
    }   
}

9 个解决方案

#1


186  

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:

输出:

last character: f

#2


77  

Here is a method using String.charAt():

这里有一个使用String.charAt()的方法:

String str = "India";
System.out.println("last char = " + str.charAt(str.length() - 1));

The resulting output is last char = a.

产生的输出是最后一个char = a。

#3


14  

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);

#4


6  

The other answers are very complete, and you should definitely use them if you're trying to find the last character of a string. But if you're just trying to use a conditional (e.g. is the last character 'g'), you could also do the following:

其他的答案非常完整,如果你想找到字符串的最后一个字符,你一定要使用它们。但是,如果你只是想使用条件句(例如,是最后一个字符g),你也可以这样做:

if (str.endsWith("g")) {

or, strings

或者,字符串

if (str.endsWith("bar")) {

#5


5  

Try this:

试试这个:

if (s.charAt(0) == s.charAt(s.length() - 1))

#6


3  

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.

另一个解决方案使用StringBuilder(它通常用于执行字符串管理,因为字符串本身是不可变的。

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. ;)

在这里,完整的字符串被颠倒了,而不应该反转的部分被替换为子字符串。,)

#7


2  

public String lastChars(String a) {
if(a.length()>=1{
String str1 =a.substring(b.length()-1);
}
return str1;
}

#8


2  

public char LastChar(String a){
    return a.charAt(a.length() - 1);
}

#9


1  

String aString = "This will return the letter t";
System.out.println(aString.charAt(aString.length() - 1));

Output should be:

输出应该是:

t

Happy coding!

编码快乐!

#1


186  

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:

输出:

last character: f

#2


77  

Here is a method using String.charAt():

这里有一个使用String.charAt()的方法:

String str = "India";
System.out.println("last char = " + str.charAt(str.length() - 1));

The resulting output is last char = a.

产生的输出是最后一个char = a。

#3


14  

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);

#4


6  

The other answers are very complete, and you should definitely use them if you're trying to find the last character of a string. But if you're just trying to use a conditional (e.g. is the last character 'g'), you could also do the following:

其他的答案非常完整,如果你想找到字符串的最后一个字符,你一定要使用它们。但是,如果你只是想使用条件句(例如,是最后一个字符g),你也可以这样做:

if (str.endsWith("g")) {

or, strings

或者,字符串

if (str.endsWith("bar")) {

#5


5  

Try this:

试试这个:

if (s.charAt(0) == s.charAt(s.length() - 1))

#6


3  

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.

另一个解决方案使用StringBuilder(它通常用于执行字符串管理,因为字符串本身是不可变的。

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. ;)

在这里,完整的字符串被颠倒了,而不应该反转的部分被替换为子字符串。,)

#7


2  

public String lastChars(String a) {
if(a.length()>=1{
String str1 =a.substring(b.length()-1);
}
return str1;
}

#8


2  

public char LastChar(String a){
    return a.charAt(a.length() - 1);
}

#9


1  

String aString = "This will return the letter t";
System.out.println(aString.charAt(aString.length() - 1));

Output should be:

输出应该是:

t

Happy coding!

编码快乐!