Reputation: 1856
I'm trying to make a Minecraft Server control panel, and I want to get a list of all online players, each username in it's own String. The way you get the users is typing /list and it returns a string. the returned string looks like:
[HH:MM:SS] INFO: username1, username2, username3, and username4
so, how would i extract each username into it's own string? I've googled this, and looked as similar questions, and I cant find a useful answer. I thought about string.replaceAll(); but i cant seem to get that to work.
Any suggestions?
Upvotes: 0
Views: 128
Reputation: 30136
The previous answer with a few more technical details:
String[] usernames = String.split(",");
In order to extract the first username, you'll have to do something like:
String username1 = usernames[0].split("INFO:")[1]; // not sure if you need ":" or "\:", so check it out...
This is because usernames[0] == "[HH:MM:SS] INFO: username1"
, and you want to split it into the sub-string that appears before "INFO:" and the sub-string that appears after it.
In order to extract the remaining usernames, just iterate the usernames
array from index 1.
For example:
for (int i=1; i<usernames.length; i++)
System.out.println(usernames[i]);
Note: you might want to strip off leading and/or trailing spaces, using strip()
.
Upvotes: 1
Reputation: 382
Adding to what lucian said:
String [] data = s.split(", ");
data[0]=data[0].replaceAll("[HH:MM:SS] INFO: ", "");
Should replace the unwanted beginning of that string with nothing ("");
Upvotes: 0
Reputation: 441
try using
String.split(", ");
This will split the string to an array.
Here is how> tutorial
String usernames = ...; // fill with data
String[] data = usernames.split(", ");
Afther this you must remove the date and time from the first name.
Upvotes: 1