从字符串中删除定义的零件 [英] Remove a defined part from a string

查看:145
本文介绍了从字符串中删除定义的零件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个字符串:

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


推荐答案

p>

Try this Regex:

public static string Remove(string text)
{
    return  Regex.Replace(text, "<.*?>","");
}

这篇关于从字符串中删除定义的零件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆