gpt4 book ai didi

java - 从任何字符串中获取最后三个字符 - Java

转载 作者:太空宇宙 更新时间:2023-11-04 09:53:53 25 4
gpt4 key购买 nike

我试图获取任何字符串的最后三个字符并将其保存为另一个字符串变量。我的思维过程遇到了一些困难。

String word = "onetwotwoone"
int length = word.length();
String new_word = id.getChars(length-3, length, buffer, index);

当涉及到缓冲区或索引时,我不知道如何使用 getChars 方法。 Eclipse 让我拥有了那些。有什么建议吗?

最佳答案

为什么不只是String substr = word.substring(word.length() - 3)

更新

在调用 substring() 之前,请确保检查 String 的长度至少为 3 个字符:

if (word.length() == 3) {
return word;
} else if (word.length() > 3) {
return word.substring(word.length() - 3);
} else {
// whatever is appropriate in this case
throw new IllegalArgumentException("word has fewer than 3 characters!");
}

关于java - 从任何字符串中获取最后三个字符 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54383580/

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