简体   繁体   English

我需要在字符串中查找特殊字符并将其显示在C#的标签中

[英]I need to find Special Characters in a string and display them in a label in C#

I need to find Characters contained in a string. 我需要找到字符串中包含的字符。 anything other than AZ(lower and upper) or numbers or () or / , those are the only characters allowed. AZ(lower and upper) or numbers or () or /其他所有字符都是允许的。

anything else (especially à,è,ë,Ç,ç,ï and so forth )are crucial to find and display in a label. 其他任何东西(尤其是à,è,ë,Ç,ç,ï and so forth )对于找到并显示在标签中都是至关重要的。

Basically I Stored everything from a text file to a string,using streamreader. 基本上,我使用streamreader存储了从文本文件到字符串的所有内容。 now every character other than [a-zA-z0-9]()/,need to be displayed in a label. 现在,除了[a-zA-z0-9]()/以外的每个字符都需要显示在标签中。

the text file will look something like this: 文本文件将如下所示:

0145 Joane 5521 Maxin 0145乔恩5521马克辛
0211 ChanÉ 2145 Spur 0211ChanÉ2145马刺
0124 Martôn 4512 btgames, 0124Martôn4512 btgames,

private void button1_Click(object sender, EventArgs e)
    {

        string BodyToCheck = richTextBox1.Text;

      //This is where I need coding....
  }




    private void Form1_Load(object sender, EventArgs e)
    {

        StreamReader reader = new StreamReader("C:/Users/Quinell.Struthers/Test.txt");

        string body = reader.ReadToEnd();
        reader.Close();
        reader.Dispose();

        richTextBox1.Text = body;
    }

好了,您可以使用正则表达式替换来删除允许的字符,然后显示标签中所有不允许的字符:

label1.Text = Regex.Replace(textBox1.Text, "(?i)[a-z0-9()/]", "");
public static string GetSpecialCharacters(string str) {
   StringBuilder sb = new StringBuilder();
   foreach (char c in str) {
      if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') | || (c >= 'a' && c <= 'z') | c == '.' || c == '_')) {
         sb.Append(c);
      }
   }
   return sb.ToString();
}
using System.Text.RegularExpressions;
using System.Linq;
var regex=new Regex("[^A-Za-z0-9()/]");
var toDisplay=string.Join("",regex.Matches(testString).OfType<Match>().Select(x=>x.Value).ToArray());

EDIT Actually Joey's answer makes it far simpler, use a replace rather than grabbing all the values 编辑实际上,乔伊(Joey)的回答使其简单得多,使用替换而不是获取所有值

using System.Text.RegularExpressions;

var regex=new Regex("[A-Za-z0-9()/]");
var toDisplay=regex.Replace(testString,string.Empty);

EDIT: Description of method 2 编辑:方法2的说明

The first using needs to be at the top of your file, just like other usings. 就像其他用法一样,第一个用法必须位于文件的顶部。

The next line creates a Regular Expression object Regex on MSDN 下一行在MSDN上创建一个正则表达式对象Regex

This regular expression will match any character that is in the range AZ , az or 0-9 and additional would match ( , ) and / . 此正则表达式将匹配AZaz0-9范围内的任何字符,并且其他字符将匹配()/

Finally the variable toDisplay is set to be the result of replacing all these matches with the empty string (effectively deleting them from the result). 最终,将变量toDisplay设置为用空字符串替换所有这些匹配项的结果(有效地将它们从结果中删除)。 testString is the source string you wish to filter. testString是您希望过滤的源字符串。 The code does not alter the source string merely sets toDisplay to be the result of the filter. 该代码不会更改源字符串,只是将toDisplay设置为过滤器的结果。

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

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