java中怎么截取字符串最后面的4个字符

2024-12-01 17:21:53
推荐回答(5个)
回答1:

BattleMacross正确,传一个参数就可以了,不需要其他人那么繁琐了

回答2:

String s = "jljfskjflsjdflksjdf";
String sub = s.subString(s.length ()- 4,s.length()); //最后四个字符

回答3:

public static void main(String[] args){

String str = "happy";
String strLast;
strLast = str.substring(str.length()-4);
System.out.println(strLast);

}
输出结果为:appy

回答4:

package test;

public class SplitLast4 {

/**
* @param args
* @throws UnsupportedEncodingException
*/
public static void main(String[] args){
// TODO Auto-generated method stub
String s = "ghj";
String str = new SplitLast4().splitLastFourChars(s);
System.out.println(str);
}
String splitLastFourChars(String s){
String str = "";
if(s.length()<4){
return s;
}else{
for(int i=s.length()-4;i str = str + s.charAt(i);
}
return str;
}
}
}

回答5:

String s = "111sdfsdfsdfsfsaaxiahello啊";
String s1 = s.substring(s.length()-4,s.length());
System.out.println(s1);