从 C# 中的字符串中删除括号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12841438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:39:13  来源:igfitidea点击:

Remove enclosing brackets from a string in c#

c#brackets

提问by Arun

I need to remove enclosing brackets from a string in c# in code behind. For example, if I have a string as [My [] Groups], I want to turn it into My [] Groups.

我需要在后面的代码中从 c# 中的字符串中删除括号。例如,如果我有一个字符串 as [My [] Groups],我想把它变成My [] Groups.

Thanks.

谢谢。

采纳答案by dasblinkenlight

The simplest way of addressing this using the string from your example would be taking a substring:

使用示例中的字符串解决此问题的最简单方法是采用子字符串:

if (s.Length > 2) {
    s = s.Substring(1, s.Length-2);
}

This works only when you are 100% certain that the first and the last characters are indeed square brackets. If they are not, for example, when the string is untrimmed, you may need to perform additional string manipulations (e.g. trimming the string).

这仅在您 100% 确定第一个和最后一个字符确实是方括号时才有效。如果它们不是,例如,当字符串未修剪时,您可能需要执行额外的字符串操作(例如修剪字符串)。

回答by inquam

Check out the String.Replacemethod

检查String.Replace方法

http://msdn.microsoft.com/en-us/library/fk49wtc1.asp

http://msdn.microsoft.com/en-us/library/fk49wtc1.asp

Update:

更新:

If only enclosing brackets are to be removed you can just strip away the first and last char. Either by the code suggested by dasblinkenlight or by a regular expression.

如果只删除括号,您可以删除第一个和最后一个字符。通过 dasblinkenlight 建议的代码或正则表达式。

Just make sure that they are indeed brackets first.

首先确保它们确实是括号。

If you use a regular expression you can do all of this in one go. Otherwise you should add something like this to dasblinkenlight's solution.

如果您使用正则表达式,您可以一次性完成所有这些。否则,您应该在 dasblinkenlight 的解决方案中添加类似的内容。

if (s.Length > 2) {
  if(s.StartsWith("[")) {
    s = s.Substring(1, s.Length-1);
  }
  if(s.EndsWith("]")) {
    s = s.Substring(0, s.Length-1);
  }
}

or if you only wish to strip if you have both a startingand a closingbracket

或者,如果您只希望在有起始括号和结束括号的情况下剥离

if (s.Length > 2) {
  if(s.StartsWith("[") && s.EndsWith("]")) {
    s = s.Substring(1, s.Length-2);
  }
}

The check for if the length is greater than 2 can probably be removed to, but I kept it in to show that the source of the initial code was the one written by dasblinkenlight.

检查长度是否大于 2 可能可以删除,但我保留它以表明初始代码的来源是由 dasblinkenlight 编写的。

回答by Artem Vyshniakov

var myString = "[My Groups]";
myString.Replace("[", string.Empty);
myString.Replace("]", string.Empty);

回答by Nasreddine

Try this:

尝试这个:

yourString = yourString.Replace("[", string.Empty).Replace("]", string.Empty);

Updated answersince the question was edited:

自编辑问题以来更新了答案

string s = "[My [] Groups]";
string pattern = @"^(\[){1}(.*?)(\]){1}$";
Console.WriteLine(Regex.Replace(s, pattern, "")); // will print My [] Groups

回答by chridam

The following statement removes all characters [ and ].

以下语句删除所有字符 [ 和 ]。

Regex.Replace("This [is [a] [test.", @"[\[\]]", "")   // -> "This is a test."

回答by djs

The Replace solutions will get rid of the internal brackets as well as the external ones. I think you want:

替换解决方案将摆脱内部括号以及外部括号。我想你想要:

string result = "[My [] Groups]".TrimLeft('[').TrimRight(']');

回答by cuongle

Simple to use Trim:

使用简单Trim

var result = "[My [] Groups]".Trim('[', ']');