Umair A.
Umair A.

Reputation: 6863

String formatting using C#

Is there a way to remove every special character from a string like:

"\r\n               1802 S St Nw<br>\r\n                    Washington, DC 20009"

And to just write it like:

"1802 S St Nw, Washington, DC 20009"

Upvotes: 1

Views: 320

Answers (6)

mdm20
mdm20

Reputation: 4563

Maybe something like this, using ASCII int values. Assumes all html tags will be closed.

public static class StringExtensions
{
    public static string Clean(this string str)
    {   
        string[] split = str.Split(' ');

        List<string> strings = new List<string>();
        foreach (string splitStr in split)
        { 
            if (splitStr.Length > 0)
            {
                StringBuilder sb = new StringBuilder();
                bool tagOpened = false;

                foreach (char c in splitStr)
                {
                    int iC = (int)c;
                    if (iC > 32)
                    {
                        if (iC == 60)
                            tagOpened = true;

                        if (!tagOpened)
                               sb.Append(c);

                        if (iC == 62)
                            tagOpened = false;
                    }
                }

                string result = sb.ToString();   

                if (result.Length > 0)
                    strings.Add(result);
            }
        }

        return string.Join(" ", strings.ToArray());
    }
}

Upvotes: 0

Ruel
Ruel

Reputation: 15780

System.Text.RegularExpressions.Regex.Replace("\"\\r\\n                                                            1802 S St Nw<br>\\r\\n                                                            Washington, DC 20009\"", 
 @"(<br>)*?\\r\\n\s+", "");

Upvotes: 0

abatishchev
abatishchev

Reputation: 100238

To remove special characters:

public static string ClearSpecialChars(this string input)
{
    foreach (var ch in new[] { "\r", "\n", "<br>", etc })
    {
        input = input.Replace(ch, String.Empty);
    }
    return input;
}

To replace all double space with single space:

public static string ClearDoubleSpaces(this string input)
{
    while (input.Contains("  ")) // double
    {
        input = input.Replace("  ", " "); // with single
    }
    return input;
}

You also may split both methods into a single one:

public static string Clear(this string input)
{
    return input
        .ClearSpecialChars()
        .ClearDoubleSpaces()
        .Trim();
}

Upvotes: 5

elsni
elsni

Reputation: 2053

You can use the C# Trim() method, look here:

http://msdn.microsoft.com/de-de/library/d4tt83f9%28VS.80%29.aspx

Upvotes: 0

Bernard
Bernard

Reputation: 7961

Use the Regex.Replace() method, specifying all of the characters you want to remove as the pattern to match.

Upvotes: 0

Muad&#39;Dib
Muad&#39;Dib

Reputation: 29196

two ways, you can use RegEx, or you can use String.Replace(...)

Upvotes: 1

Related Questions