-1

I want to be able to check for a specific character within a string that the location is being pointed at. For example in Java you would do if (b.charAt(bPointer) == '#') expect i'm coding in c#. I'm wondering if i possibly should be using the contains() method or indexOf() method or is there a different way I'm suppose to be doing this.

public static bool func(string a, string b)
        {
            int aPointer = a.Length - 1;
            int bPointer = b.Length - 1;

            int aSkips = 0;
            int bSkips = 0;

            while(aPointer >= 0 || bPointer >=0)
            {
                while (aPointer >= 0)
                {
                    if (// check if a.CharAt(aPointer) =='#' )
                    {
                        aSkips += 1;
                        aPointer -= 1;
                    }
                    else if (aSkips > 0)
                    {
                        aPointer -= 1;
                        aSkips -= 1;
                    }
                    else
                    {
                        break;
                    }
                }

                while (bPointer >= 0)
                {
                    if ()
                    {
                        bSkips += 1;
                        bPointer -= 1;
                    }
                    else if (bSkips > 0)
                    {
                        bPointer -= 1;
                        bSkips -= 1;
                    }
                    else
                    {
                        break;
                    }
                }
gunr2171
  • 16,104
  • 25
  • 61
  • 88
WoterMelan
  • 63
  • 8

1 Answers1

1

Use the indexer:

if (a[aPointer] == '#')

You may want to check the length of the string to avoid getting an IndexOutOfRangeException:

if (aPointer < a.Length && a[aPointer] == '#')
mm8
  • 163,881
  • 10
  • 57
  • 88