开发者

Removing unwanted characters from a string

开发者 https://www.devze.com 2023-02-25 04:37 出处:网络
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 g

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开发者_StackOverflow\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"


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);


using 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. ]


    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();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消