在Java中如何交换字符串的第一个和最后一个字母?

4

我需要编写一个方法,将字符串的第一个和最后一个字母交换位置。例如,字符串"java"将变为"aavj"。

以下是我的代码:

import javax.swing.JOptionPane;

public class printTest {
    public static void main(String[] args) {
        String password;
        password = JOptionPane.showInputDialog("Please input your password");
        int length = password.length();
        String firstChar = password.substring(0, 1);
        String lastChar = password.substring(length - 1);

        password = lastChar + password + firstChar;
        JOptionPane.showMessageDialog(null, password);
    }
}

使用这段代码,当我输入“java”时,输出结果是“ajavaj”,那么我该如何修复它?我需要交换前两个字母并仍保留字符串的中间部分。我应该怎么做?

6个回答

7

在这一行中,您需要截取密码:

password = lastChar + password.substring(1, length-1) + firstChar;

3
通过password = lastChar + password + firstChar;,您正在将原始密码字符串与另外两个字符串即lastCharfirstChar连接起来。通过这样做,实际上会获得一个新的字符串,其中附加了lastCharfirstChar,而不是交换它们。
另外,字符串是不可变的,每次尝试操作它时,都会创建一个新的字符串。为避免此问题,应该使用char数组。
请尝试以下代码片段:
import javax.swing.JOptionPane;
public class printTest
{
     public static void main(String[] args)
     {
          /* Capture Password */
          String password = JOptionPane.showInputDialog("Please input your password");
          char[] pass = password.toCharArray();

          /* Swap Logic */
          char temp = pass[0];
          pass[0] = pass[pass.length - 1];
          pass[pass.length - 1] = temp;

          /* Show MessageDialog */
          JOptionPane.showMessageDialog(null, new String(pass));
     }
}

请解释为什么您认为这段代码是正确的,以及它为何能够正常工作。 - Makoto
@Markoto 添加了额外的信息。为什么被踩了? - user2004685
你对char数组的解释不太合理,因为你无论如何都要创建一个新的字符串。虽然可以说这样做可以少创建一个字符串实例,但这并不足以成为在这里使用char数组的有力论据。 - Makoto
@Makoto 谢谢您的回复。但实际上我们正在创建两个较少的字符串实例。password = lastChar + password + firstChar; 除了原始代码中的 firstCharlessChar 之外,还创建了一个新的字符串。 - user2004685
从性能上来说,这可能更好。但 JVM 可以足够聪明地优化其他“substring”解决方案,就像优化这个一样快。实际上,这可能不是什么大问题。 - ZhongYu

2
我认为这应该就可以解决问题了:
import javax.swing.JOptionPane;
    public class printTest
    {
     public static void main(String[] args)
     {
     String password;
    password = JOptionPane.showInputDialog("Please input your password");
    int length = password.length();
      String password_changed = password.substring(1, password.length() - 1);
      String firstChar = password.substring(0,1);
      String lastChar = password.substring(length - 1);



      password =  lastChar +  password_changed + firstChar;
      JOptionPane.showMessageDialog(null, password);
     }
    }

您可以创建一个额外的字符串(在此示例中为password_changed),其中删除密码变量的第一个和最后一个字母。然后,您可以使用该新变量来在最后更改密码变量。


1
char c[] = password.toCharArray();
char temp = c[0]
c[0] = c[password.length-1];
c[password.length-1] = temp;

你在调换那两个字母。 c[0] 是你的第一个字母,将其存储在一个临时变量中,然后修改 c[0](你的第一个字母)的值为 c[password.length-1](你的最后一个字母)的值,然后再将这个值修改为存储在临时变量中的值。


我修改了你的答案,因为声明一个字符数组是不鼓励的。 - Idos
1
@Idos:我撤销了你的编辑,因为在大局上它并不重要。 - Makoto
@Makoto,从宏观的角度来看,最终有什么是真正重要的呢? - Jorel Amthor
答案的修改改变了数组声明。是的,最好将数组声明为char[] c,但现在的方式并不会使它变得不可访问或难以理解。 - Makoto

1

你获取第一个字符的方式是正确的,但是获取中间或最后一个字符的方式不正确。以下是一种解决方案,展示了所有三种情况:

  String password = "123456789";
  int length = password.length();

  String firstChar = password.substring(0, 1);
  String middle = password.substring(1, length - 1);
  String lastChar = password.substring(length - 1, length);

  password = lastChar + " " + middle + " " + firstChar;
  System.out.println(password);

这将打印出:
9 2345678 1

1

和其他答案差别不大,但还有另一种方法:

password = password.charAt(password.length() - 1)
           + password.substring(1, password.length() - 1)
           + password.charAt(0);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接