我该如何修复这个字符串操作算法?

时间:2012-02-16 06:01:32

标签: c#

iv在C#中使用此算法来获取给定的字符串,删除任何空格,然后返回结果。是的,有一百万种方法可以做到这一点 但我想让我自己的算法独立于标准类库。而且,我知道有一百万种方法可以用不同的方式来表达类似的算法,但是,请在这里请光临。

如果给定字符串中的第一个字符不是空格,则算法可以正常工作。 但是如果第一个字符有空格,则返回的字符串包含 正确的字符数量,但字符是“空白”。至少在打印时 到控制台。我一直盯着这段代码几个小时,尝试这个,尝试这个,但是, 对于我的生活,我无法弄清楚导致问题的原因。我吓死了 甚至发布这个,因为有人可能会想出一个让我看起来迟钝的简单修复 然后iv每个人都有时间,但是,iv看着,看着,在这里,那里。我需要帮助! 看看代码吧。

public static string RemoveSpaces(string arg)
    {
        char[] temp = arg.ToCharArray();
        int newLength = 0;

        //Calculate number of characters that arent spaces.
        for (int e = 0; e < temp.Length; e++)
        {
            if (temp[e] != ' ')//If not a space,
            {
                newLength++;//then increment number of characters.
            }
        }

        //Now use that number as size in new array
        char[] newString = new char[newLength];

        //Copy characters that arent spaces to the new char array.
        for (uint e = 0, e2 = 0; e < temp.Length; e++, e2++)
        {
            //NOTE: e2 (and e) is a uint because it can end up negative for a 
            //short period.
            if (temp[0] == ' ')//If the FIRST element is a space
            {
                e2--;//Then dont let e2 be "truely" incremented in next cycle.
                continue;
                //e (and e2) will now be incremented by the loop.
                //but since we just decremented e2 it will be 1 behind e.
                //So we wont be skipping an element in newString if
                //we arent going to copy anything to that element.
                //Same thing happens in the other if statement below.
                //(Where the copying really happens)
            }

            if (temp[e] != ' ')//If element e is NOT a space,
            {
                newString[e2] = temp[e];//then copy that element to newString.
            }
            else//If element e IS a space
            {
                e2--;//Then dont let e2 be truely incremented in next cycle.
                //Cycle is complete so no use for continue; here.
            }
        }

        return new string(newString);//Done!
    }

5 个答案:

答案 0 :(得分:1)

不要在每次迭代中递增e2,只在角色不是空格时递增它。

试试这个:

public static string RemoveSpaces(string arg)
{
    int newLength = 0;

    //Calculate number of characters that arent spaces.
    foreach (char ch in arg)
    {
        if (ch != ' ')//If not a space,
        {
            newLength++;//then increment number of characters.
        }
    }

    //Now use that number as size in new array
    char[] newString = new char[newLength];

    //Copy characters that arent spaces to the new char array.
    int pos = 0;
    foreach (char ch in arg)
    {
        if (ch != ' ')//If element e is NOT a space,
        {
            newString[pos++] = ch;//then copy that element to newString.
        }
    }

    return new string(newString);//Done!
}

答案 1 :(得分:1)

只有在写字符时才会增加e2

答案 2 :(得分:0)

或者只是这样做:

static string RemoveSpaces(String s)
{
    StringBuilder sb = new StringBuilder();
    foreach (char c in s)
    {
        if (c != ' ')
        {
            sb.Append(c);
        }
    }
    return sb.ToString();
}

答案 3 :(得分:0)

我知道你不需要内置算法,但str = str.Replace(" ", "");是最容易记住的“现实生活”。另外,我想提请你注意空格不仅仅是' '。这些是C#中的所有空格:char [] spaces = { ' ', '\b', '\t', '\v', '\f', '\n', '\r' };(最后两个是换行符)。

答案 4 :(得分:0)

你为什么不使用

MyString.Replace(" ", "");

??????