[英]how do u get the last character of the last string in java?
I need to get the last character of the last input string. 我需要获取最后一个输入字符串的最后一个字符。 I wrote a piece of code that gets the last letter of the first string:
我写了一段代码,获取第一个字符串的最后一个字母:
public class LastCharacter {
public static void main(String[] args) {
String string = args[0];
System.out.println("last character: " + string.substring(string.length() -1));
}
}
How can I get the last character of the last string parameter? 如何获取最后一个字符串参数的最后一个字符?
You may try like this:- 你可以尝试这样: -
String str= args[args.length-1];
And to get the last character:- 并获得最后一个角色: -
char ch = str.charAt(str.length() - 1);
The last string will be at args.length - 1
in the args
array, since arrays are 0-based in Java. 最后一个字符串将位于
args
数组中的args.length - 1
,因为数组在Java中基于0。 This is also true in general for any array; 对于任何阵列来说,这也是正确的; the last element is always at
array.length - 1
. 最后一个元素总是在
array.length - 1
。
So you can do: 所以你可以这样做:
String lastString = args[args.length - 1];
Character lastCharacter = lastString.charAt(lastString.length() - 1);
Int lenght = Args.length;
String s = args[length -1];
Thats your last string in the input 这是输入中的最后一个字符串
I really can't understand the description, but based on the title, I think you want 我真的无法理解描述,但基于标题,我想你想要
public static void main(String[] args){
if(args != null && args.length > 0){
String lastString = args[args.length-1];
if(lastString.length() > 0){
System.out.println(lastString.charAt(lastString.length() - 1));
}
}
}
To get the last string: 要获取最后一个字符串:
String last = args[args.length - 1];
To get the last character: 要获得最后一个角色:
char c = last.charAt(last.length() - 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.