Nested For Loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BRawn
    New Member
    • Jul 2010
    • 28

    #1

    Nested For Loops

    Hi,

    I'm not an expert at nested for loops and I need help with the following...

    I need to extract numeric values from a string and 'discard' any alphabetical characters in the same string. In this case, there will always only be a sequence of 2 numeric strings.

    The purpose of this is to append station using racks, should the station need expanding at a later stage. Here's what I have...

    Code:
    string stationCoordinates = stationObjects.StationXY;
                                char[] digits = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
                                int rowNumber = 0;
                                int columnNumber = 0;
                                StringBuilder numericString = new StringBuilder();
                                for (int i = 0; i < stationCoordinates.Length; i++)
                                {
                                    for (int j = 0; j < stationCoordinates.Length; j++)
                                    {
                                        if (char.IsNumber(stationCoordinates[i]))
                                        {
                                            numericString.Append(stationCoordinates[i]);
                                            rowNumber = i;
                                        }
                                    }
                                }
    What I need help with is setting the 'rowNumber' and 'columnNumber' variables with the values i and j return, respectively. I want i to be 'rowNumber' and j to be 'columnNumer'.

    Lets say, for instance, stationCoordina tes' value is "Row33Col44 ", I want the 33 (i) and 44 (j), which value's should be incremented accordingly in my append method. Any ideas?
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    Ok, I hope I've understood your question: you have lets say stationCoordina tes="Row33Col44 " and you want to set two variables, i and j, e.g. i=33 and j=44.

    I'd loop over all chars in stationCoordina tes with foreach(char ch in stationCoordina tes).
    Now I'd check ch if it is a valid number. When it is, I'd append it to a temporary string containing digits (as string), otherwise I'd omit it.
    When a non-digit is following a digit-character, I'd parse my temporary string and assign it to [i]-variable. Then the same, continuing where I assigned it to i and do the same for j.

    Code:
    string stationCoordinates = "Row33Col44";
    string temp = string.Empty;
    int i = -1, j = -1;
    foreach(char ch in (stationCoordinates + "X")) {
    //this +"X" is a hack, that after "44" there's a change to a non-digit character. Doing it better, you can additionally put the part in the following else-section outside the loop ...
      if(char.IsNumber(ch)) {
        temp += ch;
      } else {
        if(!string.IsNullOrEmpty(temp)) {
          //string is containing numbers
          if(i == -1) //i wasn't initialized yet
            i = int.Parse(temp);
          else //i was initialized, assign to j
            j = int.Parse(temp);
          temp = string.Empty; //reset temp string
        }
      }
    }

    Comment

    Working...