Answers:
如果您希望远离正则表达式,那么我想到的最简单的方法是:
string input = "User name (sales)";
string output = input.Split('(', ')')[1];
var input = "(fdw) User name (sales) safdsdf (again?)"; var output = input.Split('(', ')').Where((item, index) => index % 2 != 0).ToList();
sales
也从输入串含有)sales(
,(sales(
等等
一种非常简单的方法是使用正则表达式:
Regex.Match("User name (sales)", @"\(([^)]*)\)").Groups[1].Value
作为对(非常有趣的)评论的回应,这是同一个正则表达式,带有一些解释:
\( # Escaped parenthesis, means "starts with a '(' character"
( # Parentheses in a regex mean "put (capture) the stuff
# in between into the Groups array"
[^)] # Any character that is not a ')' character
* # Zero or more occurrences of the aforementioned "non ')' char"
) # Close the capturing group
\) # "Ends with a ')' character"
var filterRegex = new Regex(Regex.Escape("(") + "([^()]*)" + Regex.Escape(")"));
假设您只有一对括号。
string s = "User name (sales)";
int start = s.IndexOf("(") + 1;
int end = s.IndexOf(")", start);
string result = s.Substring(start, end - start);
int end = s.IndexOf(")", start);
。我已经排队进行编辑了……
正则表达式可能是这里最好的工具。如果您不熟悉它们,建议您安装Expresso-一个很棒的正则表达式小工具。
就像是:
Regex regex = new Regex("\\((?<TextInsideBrackets>\\w+)\\)");
string incomingValue = "Username (sales)";
string insideBrackets = null;
Match match = regex.Match(incomingValue);
if(match.Success)
{
insideBrackets = match.Groups["TextInsideBrackets"].Value;
}
using System;
using System.Text.RegularExpressions;
private IEnumerable<string> GetSubStrings(string input, string start, string end)
{
Regex r = new Regex(Regex.Escape(start) +`"(.*?)"` + Regex.Escape(end));
MatchCollection matches = r.Matches(input);
foreach (Match match in matches)
yield return match.Groups[1].Value;
}
int start = input.IndexOf("(") + 1;
int length = input.IndexOf(")") - start;
output = input.Substring(start, length);
regex
我认为这种方法比较优越,但是如果您想使用谦虚的方法substring
string input= "my name is (Jayne C)";
int start = input.IndexOf("(");
int stop = input.IndexOf(")");
string output = input.Substring(start+1, stop - start - 1);
要么
string input = "my name is (Jayne C)";
string output = input.Substring(input.IndexOf("(") +1, input.IndexOf(")")- input.IndexOf("(")- 1);
这是避免使用正则表达式的通用可读函数:
// Returns the text between 'start' and 'end'.
string ExtractBetween(string text, string start, string end)
{
int iStart = text.IndexOf(start);
iStart = (iStart == -1) ? 0 : iStart + start.Length;
int iEnd = text.LastIndexOf(end);
if(iEnd == -1)
{
iEnd = text.Length;
}
int len = iEnd - iStart;
return text.Substring(iStart, len);
}
要在您的特定示例中调用它,可以执行以下操作:
string result = ExtractBetween("User name (sales)", "(", ")");
我在寻找非常相似的实现方案的解决方案时遇到了这个问题。
这是我的实际代码片段。从第一个字符(索引0)开始。
string separator = "\n"; //line terminator
string output;
string input= "HowAreYou?\nLets go there!";
output = input.Substring(0, input.IndexOf(separator));
此代码比此处的大多数解决方案(如果不是全部)要快,它们打包为String 扩展方法,它不支持递归嵌套:
public static string GetNestedString(this string str, char start, char end)
{
int s = -1;
int i = -1;
while (++i < str.Length)
if (str[i] == start)
{
s = i;
break;
}
int e = -1;
while(++i < str.Length)
if (str[i] == end)
{
e = i;
break;
}
if (e > s)
return str.Substring(s + 1, e - s - 1);
return null;
}
这一点更长或更慢,但是它可以更好地处理递归嵌套:
public static string GetNestedString(this string str, char start, char end)
{
int s = -1;
int i = -1;
while (++i < str.Length)
if (str[i] == start)
{
s = i;
break;
}
int e = -1;
int depth = 0;
while (++i < str.Length)
if (str[i] == end)
{
e = i;
if (depth == 0)
break;
else
--depth;
}
else if (str[i] == start)
++depth;
if (e > s)
return str.Substring(s + 1, e - s - 1);
return null;
}