繁体   English   中英

拒绝第一个和最后一个字符?

[英]Replance first and last Char?

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

这可能有效

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

您可以尝试:

StringBuilder sb = new StringBuilder("foo bar");
System.out.println(sb); // foo bar
sb.append(sb.charAt(0));
sb.setCharAt(0, sb.charAt(sb.length() - 2));
sb.deleteCharAt(sb.length() - 2);
System.out.println(sb); // roo baf

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM