简体   繁体   中英

Issue with Removing characters from a string

I've seen many answers on how to deal with editing characters within a string with c#.

However, I need a reply which only considers using the string class method remove (also to avoid using the 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. 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. 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 :

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:

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 .

Walk the string backwards - from Length-1 to 0 .

You can also try. But I believe the Linq or Regex solutions are better options.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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