如何替换以下代码中的第一个和最后字符?我不知道该怎么做。 (我是初学者)谢谢。 (我知道我的 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/