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

后端 未结 17 1773
鱼传尺愫
鱼传尺愫 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条回答
  • 2020-11-22 15:16

    Much similar to @Gustavo Baiocchi Costa but offset is being calculated with another intermediate Substring.

    int innerTextStart = input.IndexOf("(") + 1;
    int innerTextLength = input.Substring(start).IndexOf(")");
    string output = input.Substring(innerTextStart, innerTextLength);
    
    0 讨论(0)
  • 2020-11-22 15:17

    Here is a general purpose readable function that avoids using regex:

    // 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);
    }
    

    To call it in your particular example you can do:

    string result = ExtractBetween("User name (sales)", "(", ")");
    
    0 讨论(0)
  • 2020-11-22 15:19

    The regex method is superior I think, but if you wanted to use the humble 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);
    

    or

    string input = "my name is (Jayne C)";
    string output  = input.Substring(input.IndexOf("(") +1, input.IndexOf(")")- input.IndexOf("(")- 1);
    
    0 讨论(0)
  • 2020-11-22 15:20

    A very simple way to do it is by using regular expressions:

    Regex.Match("User name (sales)", @"\(([^)]*)\)").Groups[1].Value
    

    As a response to the (very funny) comment, here's the same Regex with some explanation:

    \(             # 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"
    
    0 讨论(0)
  • 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("\\((?<TextInsideBrackets>\\w+)\\)");
    string incomingValue = "Username (sales)";
    string insideBrackets = null;
    Match match = regex.Match(incomingValue);
    if(match.Success)
    {
        insideBrackets = match.Groups["TextInsideBrackets"].Value;
    }
    
    0 讨论(0)
  • 2020-11-22 15:24
    input.Remove(input.IndexOf(')')).Substring(input.IndexOf('(') + 1);
    
    0 讨论(0)
提交回复
热议问题