简体   繁体   English

C#字符数组在特定索引处删除

[英]C# Char Array remove at specific index

Not to sure the best way to remove the char from the char array if the char at a given index is a number. 如果给定索引处的char是数字,则不能确定从char数组中删除char的最佳方法。

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 感谢所有贡献者

您可以使用Linq的功能:

return new string(tocheckTextBox.Where(c => !char.IsNumber(c)).ToArray())

This is fairly easy using Regex: 使用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). (如@Adassko所述,如果需要数字0到9,而不需要其他任何数字字符,则可以使用"[0-9]"代替@"\\d" )。

You can also do it fairly efficiently using a StringBuilder: 您还可以使用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: 您也可以使用linq:

 var result = new string("a1b2c3d4".Where(x => !char.IsNumber(x)).ToArray());

Use Regex: 使用正则表达式:

private string TextBox_CharacterCheck(string tocheckTextBox)
{
    return Regex.Replace(tocheckTextBox, @"[\d]", string.Empty);;
}

System.String is immutable. System.String是不可变的。 You could use string.Replace or a regular expression to remove unwanted characters into a new string. 您可以使用string.Replace或正则表达式将不需要的字符删除为新字符串。

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. 字符串是不可变的,这意味着您不能更改它们-您需要重写整个字符串-若要以最佳方式进行操作,则应使用StringBuilder类并Append每个字符。

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. 同时还要注意你的代码- char.IsNumber不仅检查字符0 - 9 ,它也返回true为每个数字字符,如٢ ,你可能不希望这样。

here's the full list of characters returning true : 这是返回true的字符的完整列表:

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

you should also use [0-9] rather than \\d in your regular expressions if you want only parsable digits. 如果只需要可解析的数字,则还应该在正则表达式中使用[0-9]而不是\\d

You can also use a trick to .Split your string on your character, then .Join it back. 您还可以使用技巧将字符串.Split到角色上,然后.Join其重新加入。 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 - 此代码会将无法在有效文件名中使用的任何字符替换为-

You can use LINQ to remove the char from the char array if the char at a given index is a number. 如果给定索引处的char是数字,则可以使用LINQ从char数组中删除char。

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); 

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

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