gpt4 book ai didi

java - 替换第一个和最后一个字符?

转载 作者:行者123 更新时间:2023-12-01 23:03:38 28 4
gpt4 key购买 nike

如何替换以下代码中的第一个最后字符?我不知道该怎么做。 (我是初学者)谢谢。 (我知道我的 modifiedValue = temp.substring(0, 6); 行是错误的,因为我已经在上一行中使用了它。)

public void switched() {
String temp;
String modifiedValue;

temp = inputField.getText();
modifiedValue = temp.substring(0, 6);
outputArea.append("With first char last and last char first:\n");
outputArea.append("\t" + modifiedValue + "\n");
outputArea.append("\n");

} // end of switched()

最佳答案

要交换字符串的第一个和最后一个字符,请尝试以下操作:

int n = temp.length();
temp = temp.charAt(n-1) + temp.substring(1, n-1) + temp.charAt(0);

关于java - 替换第一个和最后一个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23147189/

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