从C#中的字符串中选择一个子字符串

时间:2012-04-05 17:24:28

标签: c#

如何从C#中最合理的字符串中选择一个子字符串,从空格到符号'#'? 例如:

"Jungle #welcome to the jungle"

结果:#welcome

2 个答案:

答案 0 :(得分:2)

string originalString = "Jungle #welcome to the Juncle";
string subString = originalString.Substring(originalString.IndexOf("#"));
subString = subString.Substring(0, subString.IndexOf(" "));

答案 1 :(得分:1)

using System.Text.RegularExpressions;

Match m = Regex.Match("Jungle #welcome to the jungle", @"\s(#\w+?)\s");
Console.WriteLine(m.Captures[0].Value);
// #welcome