how to extract unknown names out of a string - java

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?

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.

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().

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 ("");

Related

Replace data between special character

My text file has a pattern and it's just like the following:
1;Mary Yeah;John Freeman;(12)3456-7890;iammary#gmail.com
2;Ash Wilson;One Two Three;(99)1111-2222;lorddragon#hotmail.com
3;Xin Zhao;Street Address 55;(11)0101-0202;lolyourface#gmail.com
4;My Name;My Address;My Phone;myemail#mail.com
I want to be able to type the line number, the type of data I want to replace(e-mail, phone, name), and the string I want to replace them with. The program overwrites the text.
How could I code this in Java?
The issue of how to find a given row based on the line number depends on many things, most importantly it depends on code you haven't shown us. But as for what you can do once you have found a given line, you may try the following:
String line = "2;Ash Wilson;One Two Three;(99)1111-2222;lorddragon#hotmail.com";
String[] parts = line.split(";");
parts[4] = "some.address#mail.com"; // to change the email
// now join back to a single line
line = String.join(";", Arrays.asList(parts));
Demo

Replace String with characters with substring

I tried to make security to display email data by replacing some words with symbol (*) but not as expected there might be an error in making the example script as below.
String email = "thismyemail#myhost.com";
String get_text = email.get_text(3, 6);
String hasil = email.replace(get_text,"*");
email_string = (EditText) findViewById(R.id.emailT);
email_string.setText(hasil);
But the result is like this
thi*email#myhost.com
Which I expect
thi***email#myhost.com
String hasil = email.replace(get_text,"***");
But please note that if that text appears anywhere else in the string it will be replaced as well.
Also, if the email is like jf#mymailserver.com you won't be replacing a part of their user id with *.
So you can probably find a better way to select the characters, taking into account email length and also not "replacing" text but rather putting those chars at the specific position you want to.
See this related question for some ideas on how to improve this:
masking of email address in java
Your code seems right. If ur expected output is like the one mentioned above, you can just add 2 more "*" to the code.
String hasil = email.replace(get_text,"***");
I hope it helps

Regex *or other method* to sort through entire URL that's all text

This link: http://www.otc.edu/GEN/schedule/all_classes_fall.txt contains classes for my college, and I am trying to take all of this data and store it in a ClassInformationFall object I have created. Basically, the classes begin at the class title in the format like this : "ABR-100-101" and have class instructor, days it occurs, start/end time, etc.
I have written some regex to pick out the class title, and some of the easier things like start and ending time, but I have been struggling on trying to get the rest of it out. I was thinking about setting up some code where anytime another class title is encountered, it adds the following text to a new ClassInformationFall object, which I am storing in a list of that type. Even if I had that, though, I still haven't been able to successfully extract all of the data for all of the things that make up the class.
What would be the regex to pick this information out, or is regex even the way to go?
Thanks for any help, this has stumped me for awhile.
PS - I am developing the application using this in Java.
If the fields are always in the same order, you can just split each line by tabs and deal with the resulting array.
String line = bufferedReader.readLine();
while (line != null) {
String[] data = line.split("\\t+");
String name = data[0];
String credits = data[2];
String description = data[3];
String professor = data[11];
ClassInfo ci = new ClassInfo(name, credits, description, professor);
classInfoList.add(ci);
line = bufferedReader.readLine();
}

Java: reading a string in a particular format

I am not posting any code I am struck with. I am trying this in Java:
Issue:
I have words like:
,xxxx-1223
yyyyy,xxdd-345
$,xxxxr-7
sdsdsdd-18
so what ever format I have I should be able to read the last one:
xxxx-1223
xxdd-345
xxxxr-7
sdsdsdd-18
what so may be the words, all I need to to get the words as shown.
Use String#lastIndexOf(int) to find where the last comma occurs, and use String#substring(int) to get the rest of the string that follows.
String input = /* whatever */;
int lastComma = input.lastIndexOf(',');
String output = input.substring(lastComma + 1);
String[] str=yourWord.split(",");
String output=str[str.length-1];
You can use this Regex: -
(\\w+-\\d+)$
Or this specific problem can simply be solved using String.split() or String.substring(int) methods

How to trim text out of an AutoCompleteTextView after it is selected by a user

here is what I'm trying to do. I have a list of stock symbols located in the string.xml file in an android project. The list looks something like this...
ACE - ACE Limited
ABT - Abbott Laboratories
ANF - Abercrombie and Fitch Company etc...etc.
I have this list set up in the android Main as an AutoComplete array. The problem is that when the user selects one of the dropdown stocks, the box fills in the STOCK SYMBOL + the COMPANY NAME. I need to "trim" off the "company name" when the user selects it so only the stock "symbol" appears in the box. Is there a simple function or command to do this? I get confused trying to convert the array back to a string and then back again. Any help would be appreciated!
Try separating them within your XML file. For example, instead of something like
<company>ACE - ACE Limited</company>
Use
<company><symbol>ACE</symbol><name>ACE Limited</company>
or
<company symbol="ACE" name="ACE Limited" />
Then you can read the individual properties or sub-tags with your reading engine. For a simple tutorial on this, check out this link.
EDIT: If this would require too much work, you could try simply splitting the strings (assuming they all have a common delimiter of -.
String symbol;
String name;
String xmlData = "ACE - ACE Limited";
String[] splitData = xmlData.split(" - ");
symbol = splitData[0];
// Set the name to the remaining items
for (int i=1; i<splitData.length; i++) {
name += splitData[i] + " - ";
}
This will set the symbol to the first part of xmlData (or the whole string if " - " is not found) and the name to the rest of it, including all occurrences of " - ".
(Of course, you'll only want to do either of these once the user selects the item. I'm assuming your question is about the parsing of the String rather than the click event.)

Categories

Resources