How do I extract text that lies between parentheses (round brackets)?

后端 未结 17 1993
鱼传尺愫
鱼传尺愫 2020-11-22 15:12

I have a string User name (sales) and I want to extract the text between the brackets, how would I do this?

I suspect sub-string but I can\'t work out

17条回答
  •  猫巷女王i
    2020-11-22 15:20

    Regular expressions might be the best tool here. If you are not famililar with them, I recommend you install Expresso - a great little regex tool.

    Something like:

    Regex regex = new Regex("\\((?\\w+)\\)");
    string incomingValue = "Username (sales)";
    string insideBrackets = null;
    Match match = regex.Match(incomingValue);
    if(match.Success)
    {
        insideBrackets = match.Groups["TextInsideBrackets"].Value;
    }
    

提交回复
热议问题