2

Lets say I have this string:

string text = "Hi my name is <crazy> Bob";

I want to take away everything within the brackets so it turns out like this:

"Hi my name is Bob". 

So for I've tried with this and I know I've been think wrong with the while-loop but I just can't figure it out.

    public static string Remove(string text)
    {
        char[] result = new char[text.Length];

        for (int i = 0; i < text.Length; i++ )
        {
            if (text[i] == '<')
            {
                while (text[i] != '>')
                {
                    result[i] += text[i];
                }
            }
            else
            {
                result[i] += text[i];
            }
        }
        return result.ToString();
    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rnft
  • 115
  • 1
  • 2
  • 12
  • look for regular expressions – mrida May 22 '14 at 09:19
  • http://stackoverflow.com/questions/1359412/c-sharp-remove-text-in-between-delimiters-in-a-string-regex – KopBuH May 22 '14 at 09:20
  • Can the input be `Hi my name is uncle Bob.`? Should it remove only `crazy` and `strange` or `uncle` as well? – Thomas Weller May 22 '14 at 09:20
  • Duplicate of http://stackoverflow.com/questions/1359412/c-sharp-remove-text-in-between-delimiters-in-a-string-regex ? – FishySwede May 22 '14 at 09:21
  • Change `result[i] += text[i];` inside `while(text[i]!='>')` loop to `i++;` to be able to pass through characters inside `'<''>'` and return `new string(result);` – Bolu May 22 '14 at 09:33

4 Answers4

11

Try this Regex:

public static string Remove(string text)
{
    return  Regex.Replace(text, "<.*?>","");
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
6

Look at this loop:

while (text[i] != '>')
{
    result[i] += text[i];
}

That will continue executing until the condition isn't met. Given that you're not changing text[i], it's never going to stop...

Additionally, you're calling ToString on a char[] which isn't going to do what you want, and even if it did you'd have left-over characters.

If you wanted to loop like this, I'd use a StringBuilder, and just keep track of whether you're "in" an angle bracket or not:

public static string RemoveAngleBracketedContent(string text)
{
    var builder = new StringBuilder();
    int depth = 0;
    foreach (var character in text)
    {
        if (character == '<')
        {
            depth++;
        }
        else if (character == '>' && depth > 0)
        {
            depth--;
        }
        else if (depth == 0)
        {
            builder.Append(character);
        }
    }
    return builder.ToString();
}

Alternatively, use a regular expression. It would be relatively tricky to get it to cope with nested angle brackets, but if you don't need that, it's really simple:

// You can reuse this every time
private static Regex AngleBracketPattern = new Regex("<[^>]*>");
...

text = AngleBracketPattern.Replace(text, "");

One last problem though - after removing the angle-bracketed-text from "Hi my name is <crazy> Bob" you actually get "Hi my name is Bob" - note the double space.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    +1 For giving the OP both the proverbial fishing lesson and a fish! –  May 22 '14 at 09:29
2

use

string text = "Hi my name is <crazy> Bob";
text = System.Text.RegularExpressions.Regex.Replace(text, "<.*?>",string.Empty);
Bolu
  • 8,696
  • 4
  • 38
  • 70
sukhi
  • 904
  • 7
  • 11
0

I recommend regex.

public static string DoIt(string content, string from, string to)
{
    string regex = $"(\\{from})(.*)(\\{to})";
    return Regex.Replace(content, regex, "");
}
Przemo
  • 81
  • 2