gpt4 book ai didi

java - 从字符串中删除除最后 n 个字符之外的所有字符

转载 作者:行者123 更新时间:2023-11-30 11:14:36 24 4
gpt4 key购买 nike

我试图删除除最后 10 个字符之外的所有文本,但在 android 中出现错误

 09-15 16:22:58.146: E/AndroidRuntime(13630): java.lang.StringIndexOutOfBoundsException: length=228; index=263

但是当我在单独的类中尝试它时,它正在 java 上工作。这是我试过的?谁能告诉我关于 android 中的子字符串是否有任何问题?

String allchar = 
"PJGOGROG fnkmslkjsfsdmnsgdnklnsgklsgknlgrf jkghijgdlkjkgjdfkjgf kjlgdfkjlfdgfjklklj
kljfdkjlfkjldfdkjslkljdfskjlfsjkldfsjklkljs
fdsjklsdfkljsfdkljkhgfhhgdfsgkhdfskghfdskghfdsghsfdghafevbhfsvgydcgubcdmgycdgfehkhfeghjgh68
Alias12345";

String number = "";
String reqtext = allchar.replaceAll("\\s+","");
number = reqtext.substring(reqtext.length()-10 );
System.out.println(number);

最佳答案

好吧,如果 substring() 不起作用,您可以尝试手动执行:

String allchar = 
"PJGOGROG fnkmslkjsfsdmnsgdnklnsgklsgknlgrf jkghijgdlkjkgjdfkjgf kjlgdfkjlfdgfjklklj
kljfdkjlfkjldfdkjslkljdfskjlfsjkldfsjklkljs
fdsjklsdfkljsfdkljkhgfhhgdfsgkhdfskghfdskghfdsghsfdghafevbhfsvgydcgubcdmgycdgfehkhfeghjgh68
Alias12345";

StringBuilder number = new StringBuilder();
String reqtext = allchar.replaceAll("\\s+","");
for(int i = reqtext.length()-10; i < reqtext.length(); i++)
{
number.append(reqtext.charAt(i));
}
System.out.println(number.toString());

关于java - 从字符串中删除除最后 n 个字符之外的所有字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25846722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com