【发布时间】:2014-05-22 09:16:39
【问题描述】:
假设我有这个字符串:
string text = "Hi my name is <crazy> Bob";
我想去掉括号内的所有东西,结果是这样的:
"Hi my name is Bob".
所以我已经尝试过了,我知道我一直认为 while 循环有误,但我就是想不通。
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();
}
【问题讨论】:
-
查找正则表达式
-
输入可以是
Hi my name is <crazy> uncle <strange> Bob.吗?是否应该只删除crazy和strange或uncle? -
将
result[i] += text[i];内部while(text[i]!='>')循环更改为i++;以便能够通过'<''>'内部的字符并返回new string(result);