简体   繁体   English

从字符串中删除不需要的字符

[英]Removing unwanted characters from a string

I have a program where I take a date from an RSS file and attempt to convert it into a DateTime . 我有一个程序,我从RSS文件中获取日期,然后尝试将其转换为DateTime Unfortunately, the RSS file that I have to use has a lot of spacing issues. 不幸的是,我必须使用的RSS文件存在很多间距问题。 When I parse the string I get this: 当我解析字符串时,我得到以下信息:

"\t\t\n\t\t4/13/2011\n\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t"

I want to remove all of the \\t 's and \\n 's. 我要删除所有\\t\\n So far these have all failed: 到目前为止,所有这些都失败了:

finalDateString.Trim('\t');
finalDateString.Trim('\n');
finalDateString.Trim();
finalDateString.Replace("\t", "");
finalDateString.Replace("\n", "");
finalDateString.Replace(" ", "");

Every one of the commands will return the same string. 每个命令都将返回相同的字符串。 Any suggestions? 有什么建议么?

(I tagged RSS in the case that there is an RSS reason for this) (在有RSS原因的情况下,我标记了RSS)

You need to assign the original value the Replace output. 您需要为原始值分配“ Replace输出。 You do not need to do the trim either as the replace will get rid of all of them. 您也不需要修剪,因为替换将消除所有修剪。

finalDateString = finalDateString.Replace("\t", "");
finalDateString = finalDateString.Replace("\n", "");

First, you can remove all the whitespace from your string by using a 1-character regular expression: 首先,您可以使用1个字符的正则表达式从字符串中删除所有空格:

    String finalDateTimeString = "\t\t\n\t\t4/13/2011\n\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t";
    Regex whitespaceRegex = new Regex("\\s");
    finalDateTimeString = whitespaceRegex.Replace(finalDateTimeString, "");

I just tested this, and it worked. 我刚刚对此进行了测试,并且它起作用了。

Second, I just tested calling DateTime.Parse() on your string, and it worked without even removing the whitespace. 其次,我刚刚测试了在字符串上调用DateTime.Parse() ,该方法甚至无需删除空格即可工作。 So maybe you don't even have to do that. 因此,也许您甚至不必这样做。

    String finalDateTimeString = "\t\t\n\t\t4/13/2011\n\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t";
    DateTime finalDateTime = DateTime.Parse(finalDateTimeString);
    // finalDateTime.toString() == "4/13/2011 12:00:00 AM"
    private String stringclear(String str)
    {
        String tuslar = "qwertyuopasdfghjklizxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM._0123456789 :;-+/*@%()[]!\nüÜğĞİışŞçÇöÖ"; // also you can add utf-8 chars
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.Length; i++)
        {
            if (tuslar.Contains(str[i].ToString()))  //from tuslar string. non special chars
                sb.Append(str[i]);
            if (str[i] == (char)13) // special char (enter key)
                sb.Append(str[i]);
        }

        return sb.ToString();
    }

I will use Regular expressions 我将使用正则表达式

string strRegex = @"([\s])";    
Regex myRegex = new Regex(strRegex);
string strTargetString = @"  4/13/2011    ";
string strReplace = "";

string result = myRegex.Replace(strTargetString, strReplace);

使用Regex.Replace

string result = Regex.Replace(data,"[\\t,\\n]",""));

All the previously posted answers remove all whitespace from the string, but it would be more robust to only remove leading and trailing whitespace. 以前发布的所有答案均会删除字符串中的所有空格,但仅删除开头和结尾的空格会更可靠。

finalDateTimeString = Regex.Replace(finalDateTimeString, @"^\s+", "");
finalDateTimeString = Regex.Replace(finalDateTimeString, @"\s+$", "");

[ I don't know C#, so I'm guessing at the syntax from the other posts. [我不了解C#,所以我在猜测其他文章的语法。 Corrections welcome. 欢迎更正。 ] ]

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

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