title: Java substring a local variable inside a while loop
body: <p>I have been trying to construct a while loop for looping through a string when it contains a 'pattern' i'm looking for. The string is a local variable, declared just above the while loop and I am unable to substring it within my while loop, so that each consecutive loop will look at the next part of the string.</p>
  
  <p>I would appreciate any help on how I could solve this problem</p>
  
  <p>Here's the code; just so u have the idea the onlineList usually comes as array list output e.g. [Adrian, Bob, Buddy]</p>
  
  <pre><code>                String onlineList = networkInput.nextLine();
                  //Declare a local variable for modified online list, that will replace all the strings that contain ", " "[" and "]"
                  String modifiedOnlineList = onlineList.replaceAll("\\, ", "\n").replaceAll("\\[", "").replaceAll("\\]", "");
                  //Loop the modifiedOnlineList string until it contains "\n"
                  while (modifiedOnlineList.contains("\n")) {
                      //A local temporary variable for the first occurence of "\n" in the modifiedOnlineList
                      int tempFirstOccurence = modifiedOnlineList.indexOf("\n");
                      //Obtain the name of the currently looped user
                      String tempOnlineUserName = modifiedOnlineList.substring(0, tempFirstOccurence);
                      //Substring the remaining part of the string.
                      modifiedOnlineList.substring(tempFirstOccurence + 2);
                      System.out.println(modifiedOnlineList);
  
                  }
  </code></pre>