简体   繁体   中英

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 . Unfortunately, the RSS file that I have to use has a lot of spacing issues. 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. 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)

You need to assign the original value the Replace output. 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:

    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. 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. Corrections welcome. ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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