从文本文件中删除特殊字符 [英] Remove special character from text file

查看:531
本文介绍了从文本文件中删除特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有这个文字< set>< value>< name>在一个文件中,现在我需要删除字符<和>它位于文本之间。我正在尝试使用以下代码:



  private   void  button1_Click( object  sender,EventArgs e)
{
sr = new StreamReader(sam);
textBox1.Text = sr.ReadLine();
string pattern = \( ?=并[d \ ] +,)[> \] + \ ;
string result = Regex.Replace(textBox1.Text,pattern,m = > m .Value.Replace( < ));
textBox2.Text = result;
sr.Close();
}



我对这个正则表达式感到困惑,我得到了相同的输出

< set>< ;值><名称>

请帮帮....

解决方案

从你的问题看,你想要替换<和>有空间。为此,可以使用以下代码

  string  fileText = System.IO.File.ReadAllText(文件路径); 
fileText = System.Text.RegularExpressions.Regex.Replace(fileText, (\< \\ \\| \> \) );
System.IO.File.WriteAllText(filePath,fileText);


  //  此方法在这种情况下并不适用 
// 但是当有很多不可打印的字符时可以使用它
// 需要删除,例如控制和格式化字符
public static string CleanText( string 消息, char [] prohibitedCharacters)
{
List< char> messageList = message.ToCharArray()。ToList();
messageList.RemoveAll(c = > (Array.IndexOf(prohibitedCharacters,c)!= -1));
string cleaningString = string .Join( string .Empty,messageList);
return cleaningString;
}


您可以使用以下自定义功能清除特殊字符:



  private   static   string  GetCleanString( string  src)
{
int i = 0 ;
while (i < src.Length)
{
if (src [i] < 32 || src [i] > 127
{
int pos = CHAR_REPLACE_SRC.IndexOf(src [i]);
if (pos > = 0
src = src.Replace(CHAR_REPLACE_SRC [pos],CHAR_REPLACE_DST [pos]);
else
src = src.Remove(i, 1 );
}
else
i ++;
}
return src.Replace( \ )。替换( )。替换( )。替换( & _)。替换( \\ _)。替换( / _);
}


Hi I have this text "<set><value><name>" in a file, now I need to remove the character "<" and ">" which is in between the text. I’m trying with the below code:

private void button1_Click(object sender, EventArgs e)
{
sr = new StreamReader(sam);
 textBox1.Text = sr.ReadLine();
string pattern = "\"(?=[<\"]+,)[>\"]+\"";
                string result = Regex.Replace(textBox1.Text, pattern, m => m.Value.Replace("<", " "));
                textBox2.Text = result;
sr.Close();
}


I’m confused with this regular expression and I’m getting the same output
"<set><value><name>"
please help....

解决方案

As seen from your question, you want to replace "<" and ">" with a space. For this purpose the following code can be used

string fileText = System.IO.File.ReadAllText(filePath);
fileText = System.Text.RegularExpressions.Regex.Replace(fileText,"(\"<\"|\">\")"," ");
System.IO.File.WriteAllText(filePath,fileText);


//This method is not really applicable in this case
// But it can be used when there are a lot of non-printable characters
//that need to be removed, such as control and formatting characters
public static string CleanText(string message, char[] prohibitedCharacters)
{
   List<char> messageList = message.ToCharArray().ToList();
   messageList.RemoveAll(c => (Array.IndexOf(prohibitedCharacters, c) != -1));
   string cleanedString = string.Join(string.Empty, messageList);
    return cleanedString;
}


You can clear the special characters using the below customized function as:

private static string GetCleanString (string src)
{
    int i = 0;
    while (i < src.Length)
    {
        if (src[i] < 32 || src[i] > 127)
        {
            int pos = CHAR_REPLACE_SRC.IndexOf(src[i]);
            if (pos >= 0)
                src = src.Replace(CHAR_REPLACE_SRC[pos], CHAR_REPLACE_DST[pos]);
            else
                src = src.Remove(i, 1);
        }
        else
            i++;
    }
    return src.Replace("\"", "").Replace("?", "").Replace(":", "").Replace("&", "_").Replace("\\", "_").Replace("/", "_");
}


这篇关于从文本文件中删除特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆