简体   繁体   English

从字符串中删除字符的问题

[英]Issue with Removing characters from a string

I've seen many answers on how to deal with editing characters within a string with c#. 我已经看到了许多有关如何使用c#编辑字符串中的字符的答案。

However, I need a reply which only considers using the string class method remove (also to avoid using the stringbuilder). 但是,我需要一个只考虑使用字符串类方法删除的回复(也是为了避免使用stringbuilder)。

This is only so that I may see what the issue is with this specific code, which does not seem to get rid of a middle whitespace in a phone number. 这只是为了让我可以看到这个特定代码的问题是什么,它似乎没有摆脱电话号码中的中间空白。 The code also removes any other character which is not a number. 该代码还删除了不是数字的任何其他字符。

I have been unable so far to understand why the whitespace or two adjacent whitespaces would not be removed. 到目前为止,我一直无法理解为什么空白或两个相邻空白不会被删除。 I suspect maybe the self assignment to the same string might be an issue, but I'm unsure. 我怀疑也许对同一个字符串的自我分配可能是一个问题,但我不确定。

//leave only the digits
for (int i = 0; i < enteredPhone.Length; i++)
{
     switch (enteredPhone[i])
     {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
           break;

        default:
             //gets rid of any other type of character
             enterPhone = enteredPhone.Remove(i, 1);
             break;
        }
   }

Thanks in advance. 提前致谢。

Try to subtract 1 when you remove a caracter. 删除角色时,尝试减1。 Because if you remove the caracter in position 5 and i++ , the value of i now is 5, and the next caracter will be not found by the loop. 因为如果删除位置5和i ++上的角色,i的值现在为5,则循环将找不到下一个角色。 Like this: 像这样:

default:
         //gets rid of any other type of character
         enterPhone = enteredPhone.Remove(i, 1);
         i--;
         break;

Your problem is incrementing index every time, even if you have removed non-digit character. 您的问题是每次都增加索引,即使您删除了非数字字符也是如此。 In that case next character will have current index, but on next loop you are incrementing current index, thus skipping next character. 在这种情况下,下一个字符将具有当前索引,但在下一个循环中,您将递增当前索引,从而跳过下一个字符。 Use while instead of for : 使用while代替for

int index = 0;
while(index < enteredPhone.Length)
{
    switch (enteredPhone[index])
    {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            index++;
            break;

        default:                
            enteredPhone = enteredPhone.Remove(index, 1);
            break;
    }
}

Also you can use Char.IsDigit to simplify this loop: 您也可以使用Char.IsDigit简化此循环:

int index = 0;
while(index < enteredPhone.Length)
{
    if (Char.IsDigit(enteredPhone[index]))
    {
        index++;
        continue;
    }

    enteredPhone = enteredPhone.Remove(index, 1);
}

忽略你奇怪的要求:

Regex.Replace(inputString, @"\D", string.Empty)
var onlyDigits = new string(enteredPhone.Where(Char.IsDigit).ToArray());

应该比正则表达更有效率。

"why the whitespace or two adjacent whitespaces would not be removed" - because all character indices after i are reduced by 1 when you .Remove i . “为什么空白或两个相邻的空格不会被删除” -因为所有的字符索引后, i被减1,当你.Remove i

Walk the string backwards - from Length-1 to 0 . 向后走绳 - 从Length-10

You can also try. 你也可以试试。 But I believe the Linq or Regex solutions are better options. 但我相信Linq或Regex解决方案是更好的选择。

//leave only the digits
for (int i = enteredPhone.Length - 1; i >= 0; i--)
{
    if (!Char.IsDigit(enteredPhone[i]))
    {
       enterPhone = enteredPhone.Remove(i, 1);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM