0

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)

Rufus L
  • 36,127
  • 5
  • 30
  • 43
Taqwa
  • 29
  • 8
  • 3
    *"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"* That horribly complicated `for` loop can be replaced by a simple Regex. – Ron Beyer Apr 04 '18 at 18:10
  • 1
    What does your magic number `20` represent? It seems like that's the problem. Perhaps you should either `Split` first so you know the `Length` (and so you only do it once instead of every iteration), or use some other method, like `IndexOf` or, as Ron said, `Regex` – Rufus L Apr 04 '18 at 18:13
  • 20 is just a random number. Im not that familiar with Regex, can you help me with the code? Or is there a way to split and get all the returned items at once and store them in an array or something? – Taqwa Apr 04 '18 at 18:22
  • 1
    To get past your initial error, first do: `string[] items = s.Split(p1, p2);` Then you can loop over the length of the array: `for (int i = 1; i < items.Length; i += 2) { ... }` – Rufus L Apr 04 '18 at 18:22
  • Here's one way to do it: `private static bool HasRef(string s, char p1, char p2) { var result = false; var secondIndex = -1; do { var firstIndex = s.IndexOf(p1, secondIndex + 1); if (firstIndex == -1) break; secondIndex = s.IndexOf(p2, firstIndex + 1); if (secondIndex == -1) break; var contents = s.Substring(firstIndex + 1, secondIndex - firstIndex - 1); Console.WriteLine($"Found item: {p1}{contents}{p2}"); if (contents.Any(char.IsNumber)) { result = true; } } while (s.Length > secondIndex + 1); return result; }` – Rufus L Apr 04 '18 at 18:47

0 Answers0