String subscript function

How can I get a string in brackets using a special function?

ex string "GREECE (+30)" should only return "+30"

0
source share
5 answers

There are several ways.

Common string methods:

Dim left As Integer = str.IndexOf('(') Dim right As Integer= str.IndexOf(')') Dim content As String = str.Substring(left + 1, right - left - 1) 

Regular expression:

 Dim content As String = Regex.Match(str, "\((.+?)\)").Groups[1].Value 
+5
source

For a common problem, I would suggest using Regex . However, if you are confident in the format of the input string (only one set of partners, open your finger in front of a close pair), this will work:

 int startIndex = s.IndexOf('(') + 1; string result = s.Substring(startIndex, s.LastIndexOf(')') - startIndex); 
+3
source

With regular expressions .

 Dim result as String = System.Text.RegularExpressions.Regex.Match("GREECE (+30)", "\((?<Result>[^\)]*)\)").Groups["Result"].Value; 

The code has not been verified, but I expect only compilation problems.

+1
source

You can watch regular expressions or play differently using the IndexOf() function

0
source

In Python, using the row index and slicing method:

 >>> s = "GREECE(+30)" >>> s[s.index('(')+1:s.index(')')] '+30' 
0
source

Source: https://habr.com/ru/post/918628/


All Articles