在Java中交换字符串中的几个字符?

4

我需要处理一个字符串,将其中几个字符交换以进行简单的加密。

 Input : ABCDEFGHIJKLMNOPQRSTUVWXYZ
 Output: NOPQRSTUVWXYZABCDEFGHIJKLM

在上述过程中,我将字符串分成两部分,并将第一半移动到后半部分。
final int mid = (xyz.length()+1) / 2;
String[] spstr = {
    xyz.substring(0, mid),
    xyz.substring(mid),
};
String firstMix=spstr[1]+spstr[0];
System.out.println(firstMix);

现在,我需要将前两个字符和后两个字符进行交换。
Input : NOPQRSTUVWXYZABCDEFGHIJKLM
Output: LMPQRSTUVWXYZABCDEFGHIJKNO

同时,将字符串中间左侧紧挨着的两个字符和其后面紧挨着的两个字符进行调换。

Input :  LMPQRSTUVWXYZABCDEFGHIJKNO
Output : LMPQRSTUVWXABYZCDEFGHIJKNO

在输入和输出中,“YZAB”被交换为“ABYZ”。

最有效地交换这些字符的方法是什么?我需要在此过程中转换大量字符串。在这种情况下,字符串长度不是恒定的。


4
附加说明 - 使用真正的加密算法。 - jdphenix
为什么不使用java.securityMessageDigest类 - Am_I_Helpful
创建一个方法,它以字符串和要交换的字符数作为参数,并在需要时调用它。 - Nir Alfasi
你的字符串长度是否固定?如果不是,对于长度为奇数的字符串,哪些字符被认为是“紧贴在字符串中间左侧的两个字符”? - Robby Cornelissen
@jdphenix 我知道,我可以轻易使用一个加密算法!但我的想法是要制作一个简单的加密算法。 - Jash Jacob
显示剩余9条评论
4个回答

2
我会创建一个方法,将字符串转换为数组,并使用它们的索引来操作和加密字符。

2

您可以尝试使用StringBuilder类。它提供了许多有用的方法来操作字符串,但避免了在过程中创建大量字符串对象的问题。


1

JAVACODE

最好的方法是将字符串分割为字符串数组,按照您的要求进行以下处理:

http://www.compileonline.com/compile_java_online.php用于测试代码

import java.util.Arrays;
        public class HelloWorld{

             public static void main(String []args){

                System.out.println("Hello World");

                String xyz= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                final int mid = (xyz.length()+1) / 2;

                String[] spstr = {
                    xyz.substring(0, mid),
                    xyz.substring(mid)
                };

                String firstMix=spstr[1]+spstr[0];

                String[] array = firstMix.split("");


                //Swap first and last two characters
                for(int i=1;i<3;i++)
                {
                   String temp= array[i];

                   array[i]=array[array.length-i];
                    array[array.length-i]=temp;

                }    



                String str1 = Arrays.toString(array); 
                str1 = str1.substring(1, str1.length()-1).replaceAll(",", "");

                //Swap  two characters left of middle with right of middle
                int  j=2;
                 for(int i=((array.length/2)-2);i<(array.length)/2;i++)
                {
                   String temp= array[i];
                   array[i]=array[array.length/2+j];
                   array[array.length/2+j]=temp;
                   j--;
                }   
                 String str2 = Arrays.toString(array); 
                  str2 = str2.substring(1, str2.length()-1).replaceAll(",", "");
                    System.out.println( firstMix);
                 System.out.println(str1);
                   System.out.println(str2);

             }
        }

1

这基本上是正确的,除了字符串长度为奇数时交换中间字符未定义的情况。注释应该足以理解代码。

public class Main {

    public static void main( String[] args ) {

        char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

        final int mid = ( chars.length + 1 ) / 2;


        System.out.println( "Mid: " + mid );
        System.out.println( "Start : " + new String( chars ) );

        // Pass one : Swap first half chars with last half chars
        if( ( chars.length % 2 ) == 1 ) {
            /* We swap the characters this way because the left half
             *  is one character longer than the right half. To avoid
             *  unnecessary copies we move the right character to
             *  the index before the left character and save the first
             *  character to be placed at the mid point
             */
            char first = chars[ 0 ];
            for( int l = 1, r = mid; r < chars.length; l++, r++ ) {
                chars[ l - 1 ] = chars[ r ];
                chars[ r ] = chars[ l ];
            }
            chars[ mid - 1 ] = first;
        }
        else {
            for( int l = 0, r = mid; r < chars.length; l++, r++ ) {
                swap( chars, l, r );
            }
        }

        System.out.println( "Pass 1: " + new String( chars ) );

        // Pass two: Swap first two chars with last two chars
        swap( chars, 0, chars.length - 2 );
        swap( chars, 1, chars.length - 1 );

        System.out.println( "Pass 2: " + new String( chars ) );

        // Pass three: Swap middle 4 characters.
        swap( chars, mid - 1, mid + 1 );
        swap( chars, mid - 2, mid );

        System.out.println( "Pass 3: " + new String( chars ) );
    }


    public static void swap( char[] chars, int l, int r ) {
        char tmp = chars[ l ];
        chars[ l ] = chars[ r ];
        chars[ r ] = tmp;
    }
}

我选择使用字符数组进行操作,以减少创建的对象数量。在我看来,这是您所要求的最快解决方案。如果您澄清了未定义的情况,我可以编辑代码以提供正确的输出结果。

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