-1

Not to sure the best way to remove the char from the char array if the char at a given index is a number.

private string TextBox_CharacterCheck(string tocheckTextBox)
{
    char[] charlist = tocheckTextBox.ToCharArray();
    foreach (char character in charlist)
    {
        if (char.IsNumber(character))
        {

        }

    }
    return (new string(charlist));
}

Thanks in advance.

// this is now resolved. thank you to all who contributed

Jack
  • 5
  • 4
  • 1
    Several possible solutions here : https://stackoverflow.com/questions/457453/remove-element-of-a-regular-array – PaulF Mar 18 '19 at 17:24
  • no need to use `ToCharArray()`, every string is already IEnumerable so you can simply use `foreach (var character in tocheckTextBox)` – Adassko Mar 18 '19 at 17:45

6 Answers6

4

You could use the power of Linq:

return new string(tocheckTextBox.Where(c => !char.IsNumber(c)).ToArray())
nvoigt
  • 75,013
  • 26
  • 93
  • 142
2

This is fairly easy using Regex:

var result = Regex.Replace("a1b2c3d4", @"\d", "");

(as @Adassko notes, you can use "[0-9]" instead of @"\d" if you just want the digits 0 to 9, and not any other numeric characters).

You can also do it fairly efficiently using a StringBuilder:

var sb = new StringBuilder();
foreach (var ch in "a1b2c3d4")
{
    if (!char.IsNumber(ch))
    {
        sb.Append(ch);
    }   
}

var result = sb.ToString();

You can also do it with linq:

 var result = new string("a1b2c3d4".Where(x => !char.IsNumber(x)).ToArray());
canton7
  • 37,633
  • 3
  • 64
  • 77
  • no, don't do that! `.Remove` on StringBuilder will enumerate all appended fragments to match the position you want to remove, as you added only one "fragment" it has to split it into three parts and skip the second. It's 2 times faster to Append than to Remove in this case – Adassko Mar 18 '19 at 17:31
  • Used regex, clean usage. – Jack Mar 18 '19 at 17:50
1

Use Regex:

private string TextBox_CharacterCheck(string tocheckTextBox)
{
    return Regex.Replace(tocheckTextBox, @"[\d]", string.Empty);;
}
elgaspar
  • 458
  • 3
  • 11
0

System.String is immutable. You could use string.Replace or a regular expression to remove unwanted characters into a new string.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

your best bet is to use regular expressions. strings are immutable meaning that you can't change them - you need to rewrite the whole string - to do it in optimal way you should use StringBuilder class and Append every character that you want.

Also watch out for your code - char.IsNumber checks not only for characters 0-9, it also returns true for every numeric character such as ٢ and you probably don't want that.

here's the full list of characters returning true:

0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹߀߁߂߃߄߅߆߇߈߉०१२३४५६७८९০১২৩৪৫৬৭৮৯੦੧੨੩੪੫੬੭੮੯૦૧૨૩૪૫૬૭૮૯୦୧୨୩୪୫୬୭୮୯௦௧௨௩௪௫௬௭௮௯౦౧౨౩౪౫౬౭౮౯೦೧೨೩೪೫೬೭೮೯൦൧൨൩൪൫൬൭൮൯๐๑๒๓๔๕๖๗๘๙໐໑໒໓໔໕໖໗໘໙༠༡༢༣༤༥༦༧༨༩၀၁၂၃၄၅၆၇၈၉႐႑႒႓႔႕႖႗႘႙០១២៣៤៥៦៧៨៩᠐᠑᠒᠓᠔᠕᠖᠗᠘᠙᥆᥇᥈᥉᥊᥋᥌᥍᥎᥏᧐᧑᧒᧓᧔᧕᧖᧗᧘᧙᭐᭑᭒᭓᭔᭕᭖᭗᭘᭙᮰᮱᮲᮳᮴᮵᮶᮷᮸᮹᱀᱁᱂᱃᱄᱅᱆᱇᱈᱉᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙꘠꘡꘢꘣꘤꘥꘦꘧꘨꘩꣐꣑꣒꣓꣔꣕꣖꣗꣘꣙꤀꤁꤂꤃꤄꤅꤆꤇꤈꤉꩐꩑꩒꩓꩔꩕꩖꩗꩘꩙0123456789

you should also use [0-9] rather than \d in your regular expressions if you want only parsable digits.

You can also use a trick to .Split your string on your character, then .Join it back. This not only allows you to remove one or more characters, it also lets you to replace it with some other character. I use this trick to remove incorrect characters from file name:

string.Join("-", possiblyIncorrectFileName.Split(Path.GetInvalidFileNameChars()))

this code will replace any character that cannot be used in valid file name to -

Adassko
  • 5,201
  • 20
  • 37
0

You can use LINQ to remove the char from the char array if the char at a given index is a number.

CODE

//This will return you the list of char discarding the number.
var removedDigits = tocheckTextBox.Where(x => !char.IsDigit(x)); 

//This will return the string without numbers.
string output = string.join("", removedDigits); 
Hasta Tamang
  • 2,205
  • 1
  • 18
  • 17