I am given bunch of strings in the following format:
ASDF [ 6]
ZXC[1]
OtPasd[ 4 ]
asdffa[ 7]
I need to retrieve the integer betwee
In my personal opinion the cleanest solution is to use regexes. But instead of guessing if it is computationally intensive I would rather benchmark it. Here's the code.
const int Count = 10000000;
const string testString = "<whatever>";
// Solution No. 1: use Regex.Match()
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < Count; i++)
{
var match = Regex.Match(@"\[\s*(\d+)\s*\]$", testString);
if (!match.Success)
continue;
var number = int.Parse(match.Groups[1].Value);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
// Solution No. 2: use IndexOf() and Substring() shenanigans
sw.Start();
for (int i = 0; i < Count; i++)
{
var lb = testString.IndexOf('[');
var rb = testString.LastIndexOf(']');
if (lb < 0 || rb != testString.Length - 1)
continue;
var str = testString.Substring(lb + 1, rb - lb - 1);
int number;
if (!int.TryParse(str, out number))
continue;
// use the number
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
And here are the results:
Solution | testString | Time (ms) | Comment
----------|--------------|--------------|-----------------------
1 | abc [ ] | 4476 | Invalid input string
2 | abc [ ] | 6594 | Invalid input string
1 | abc[1234] | 4446 | Valid input string
2 | abc[1234] | 6290 | Valid input string
As you can see, not only the regex solution is shorter and cleaner, it is actually faster. And if you play with different input strings you will notice that the longer your input string is, the larger the gap between the first and the second solutions.
If you want to avoid using Regex... Would using IndexOf/LastIndexOf and then parsing the remaining string be suitable for what you need?
use this regex (?m)(?!<=\[)(\[\s*)(\d+)(\s*\])(?!\])
your integer in match group
Try this regular expression:
\[\s*(\d+)\s*\]$
To get the int in between the brackets you can also try this way:
string tmpString = "ASDF [ 6]";
int start = tmpString.IndexOf('[') + 1;
int length = tmpString.IndexOf(']') - start;
string subString = tmpString.Substring(start, length);
int tempInt;
if(Int.TryParse(subString, out tempInt))
return tempInt;