如何在 c# sharp 中将 char 数组中的字符向左和向右移动

How to move chars in char array to the left and to the right in c# sharp

伙计们!我有这种情况:给定一个字符串和另外两个字符 X 和 Y。将所有 X 字符移动到字符串的开头,将所有 Y 字符移动到字符串的末尾。字符串中其他字符的顺序保持不变。 我写了两个函数 MoveCharsLeftMoveCharsRight 将 X 向左移动 Y 向右移动,但是出现错误 System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' 这行代码 char toReplace = splitText[charToCheck]; 不知道怎么处理,为了解决这个问题. 大佬们帮我看看,功能应该怎样?

static void Main()
    {
        string text = Console.ReadLine();
        char[] splitText = text.ToCharArray();
        string firstLetter = Console.ReadLine();
        char[] firstChar = firstLetter.ToCharArray();
        string secondLetter = Console.ReadLine();
        char[] secondChar = secondLetter.ToCharArray();
        char one = firstChar[0];
        char two = secondChar[0];
        Console.WriteLine(CheckChars(splitText, one, two));
        Console.ReadLine();
    }

    static char[] CheckChars(char[] splitText, char one, char two)
    {
        for (char letter = 'a'; letter <= 'z'; letter++)
        {
            if (Array.IndexOf(splitText, one) > -1)
            {
                MoveCharsLeft(splitText, one);
            }

            if (Array.IndexOf(splitText, two) > -1)
            {
                MoveCharsRight(splitText, two);
            }
        }

        return splitText;
    }

    static void MoveCharsLeft(char[] splitText, char charToCheck)
    {
        char toReplace = splitText[charToCheck];
        char currentLetter = splitText[0];
        for (int i = 0; i <= charToCheck; i++)
        {
            char temporary = splitText[i];
            splitText[i] = currentLetter;
            currentLetter = temporary;
        }

        splitText[0] = toReplace;
    }

    static void MoveCharsRight(char[] splitText, char charToCheck)
    {
        char toReplace = splitText[charToCheck];
        char currentLetter = splitText[-1];
        for (int i = 0; i <= charToCheck; i++)
        {
            char temporary = splitText[i];
            splitText[i] = currentLetter;
            currentLetter = temporary;
        }

        splitText[-1] = toReplace;
    }

你没有检查你的界限。我会说 CheckChars 方法是多余的,在 MoveCharsLeftMoveCharsRight 中你没有检查你是否还在 splitText.

以下是我的实施方式 MoveCharsLeft,希望它能为您指明正确的方向。

char[] MoveCharactersLeft(char[] text, char character) {
    var lowerCharacter = char.ToLower(character);
    var left = new List<char>();
    var right = new List<char>();
    
    foreach (var letter in text) {
        if (char.ToLower(letter) == lowerCharacter) {
            left.Add(letter);
            continue;
        }
        
        right.Add(letter);
    }
    
    left.AddRange(right);
    return left.ToArray();
}

这里是 MoveCharactersRight

char[] MoveCharactersRight(char[] text, char character) {
    var lowerCharacter = char.ToLower(character);
    var left = new List<char>();
    var right = new List<char>();
    
    foreach (var letter in text) {
        if (char.ToLower(letter) == lowerCharacter) {
            right.Add(letter);
            continue;
        }
        
        left.Add(letter);
    }
    
    left.AddRange(right);
    return left.ToArray();
}

既然你已经拥有了两者,你只需调用 MoveCharsLeft,然后对 MoveCharsLeft 的结果调用 MoveCharactersRight

您可以就地交换它们,尽管这需要大量复制。这是一个将字符从数组中的一个位置移动到另一个位置的函数。您可以在网络和 SO.

上找到几个这样的示例
    public static void Move(char[] buffer, int from, int too)
    {
        if (from == too)
            return;

        char temp = buffer[from];

        if (too < from)
        {
            Array.Copy(buffer, too, buffer, too + 1, from - too);
        }
        else
        {
            Array.Copy(buffer, from + 1, buffer, from, too - from);
        }

        buffer[too] = temp;
    }

然后你只需要遍历数组测试字符,测试它们并将它们移动到正确的位置。这是执行此操作的函数:

    public static void ArrangeChars(char[] buffer, char left, char right)
    {
        int leftIndex = 0;
        int rightIndex = buffer.Length - 1;

        int i = 0;

        while(i <= rightIndex)
        {
            char ch = buffer[i];

            if (ch == left && i > leftIndex)
                Move(buffer, i, leftIndex++);
            else if (ch == right && i < rightIndex)
                Move(buffer, i, rightIndex--);
            else
                ++i;
        }
    }

这样称呼它:

    static void Main(string[] _)
    {
        char[] data = "test me and move".ToCharArray();

        Console.WriteLine(new string(data));

        ArrangeChars(data, 'e', 't');

        Console.WriteLine("Move 'e' to start, move 't' to end");

        Console.WriteLine(new string(data));
    }

通常的注意事项适用 - 这尚未经过充分测试,如果您使用它,您应该检查所有边缘情况。