I'm trying to take a string, check if they have parentheses or brackets in them, check if those contain numbers inside, and return true if it does.
I'm using Any(char.IsDigit)
to check if there is a digit but it still returns true
if there are just letters and no digits.
Also I keep getting an error that the index is out of bounds. I will be using different strings so I can't use a fixed number for the bounds. I'm checking that it is not null
in the for loop before going to the next one, but maybe thats not how Im supposed to do it?
Code
public static void Main(string[] args)
{
string test = "Endometrial cancer is the most common gynecologic malignancy in the developed world[1], with the highest incidence in the US and Canada[2]. Endometrial biopsy plays a significant role in early cancer diagnosis, preoperative assessment, and treatment planning[3, 4], with Pipelle biopsy emerging as the most common method for sampling endometrial tissue in patients with suspected endometrial cancer[5, 6]. Pipelle sampling is a cost-effective[7] procedure and has similar sampling adequacy and histopathological results as dilation and curettage (DC)[8].";
bool brackets = hasRef(test, '[', ']');
bool parantheses = hasRef(test, '(', ')');
Console.WriteLine(brackets);
Console.WriteLine(parantheses);
}
static bool hasRef(string s, char p1, char p2)
{
bool has = false;
for (int i = 1; (i <= 20 && ((s.Split(p1, p2)[i]) != null)); i += 2)
{
string split = s.Split(p1, p2)[i];
Console.WriteLine(p1 + split);
if (split.Any(char.IsDigit))
{
has = true;
Console.WriteLine(split);
break;
}
}
return has;
}
Error
Exception in user code:
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Rextester.Program.hasRef(String s, Char p1, Char p2)
at Rextester.Program.Main(String[] args)