我一直在尝试构建一个 while 循环,以在字符串包含我正在寻找的“模式”时循环该字符串。该字符串是一个局部变量,在 while 循环上方声明,我无法在我的 while 循环中对其进行子字符串化,因此每个连续循环都将查看字符串的下一部分。
我将不胜感激有关如何解决此问题的任何帮助
这是代码;就这样你知道 onlineList 通常作为数组列表输出,例如 [Adrian, Bob, Buddy]
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);
}