0

I need to find Characters contained in a string. anything other than A-Z(lower and upper) or numbers or () or /, those are the only characters allowed.

anything else (especially à,è,ë,Ç,ç,ï and so forth)are crucial to find and display in a label.

Basically I Stored everything from a text file to a string,using streamreader. now every character other than [a-zA-z0-9]()/,need to be displayed in a label.

the text file will look something like this:

0145 Joane 5521 Maxin
0211 ChanÉ 2145 Spur
0124 Martôn 4512 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;
    }
4

3 回答 3

0
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();
}
于 2012-08-03T10:27:14.557 回答
0
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());

编辑实际上乔伊的回答使它更简单,使用替换而不是获取所有值

using System.Text.RegularExpressions;

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

编辑:方法2的描述

第一次使用需要在文件的顶部,就像其他使用一样。

下一行在MSDN 上创建一个正则表达式对象 Regex

此正则表达式将匹配范围内的任何字符A-Za-z或者0-9和附加将匹配(,)/

最后,变量 toDisplay 被设置为用空字符串替换所有这些匹配的结果(有效地将它们从结果中删除)。testString是您要过滤的源字符串。该代码不会更改源字符串,只是将 toDisplay 设置为过滤器的结果。

于 2012-08-03T10:28:31.083 回答
0

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

label1.Text = Regex.Replace(textBox1.Text, "(?i)[a-z0-9()/]", "");
于 2012-08-03T10:25:42.577 回答