2012-02-16 87 views
0

iv一直在C#中使用此算法来获取给定的字符串,删除任何空格,然后返回结果。是的,有一百万种方法可以做到这一点 但我想让我自己的算法独立于标准类库。而且,我知道有一种方法可以用不同的方式来实现类似的算法,但请在这里裸露在我身边。如果给定字符串中的第一个字符是不是空格我该如何解决这个字符串操作算法?

算法工作正常。 但是,如果有作为第一个字符的空间,返回的字符串包含的字符 正确量,但是,该字符被打印 到控制台。至少当“空白”。我一直盯着这个代码几个小时,尝试这个,试图,但是,对于我的生活,我不能找出什么导致这个问题的 。林吓死 甚至张贴此有人会想出了一个简单的修复,使我看起来迟钝 然后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! 
    } 
+0

你怎么想达到什么目的?你想删除任何额外的空间尾随和字符内?的 – 2012-02-16 06:03:44

+0

可能重复的[未记录返回时在参数的值是通是从功能](http://stackoverflow.com/questions/9113086/no-records-return-when-the-value-being-pass-in-参数是从功能) – 2012-02-16 06:05:19

回答

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! 
} 
+0

bea utifull。奇迹般有效。谢谢! – kbzombie 2012-02-16 06:26:35

1

只有增加e2当你写一个字。

0

或者只是这样做:

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

感谢您的回复,很好的信息。对其他人也应该有用。 – kbzombie 2012-02-16 06:28:16

+0

希望你在编辑之前没有读过我最后的评论,听起来很粗鲁。没有这个意思。 – kbzombie 2012-02-16 06:33:33

0

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

+0

了不起的信息,但我只想删除''空间,因为其他任何人都可能被程序员放入其中。我总是可以写取这可能是所有这些可能性枚举和开关反对,只是删除一个额外的参数的功能,那么你看到的IM与此去:) – kbzombie 2012-02-16 06:46:06

0

为什么ü没有使用

MyString.Replace(" ", ""); 

??????

+0

因为我想impliment算法自己不使用任何来自标准库:)为什么你不阅读整篇文章?????? – kbzombie 2012-02-16 08:03:11