Replace String with characters with substring - java

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

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

Check for email id in a text in java

i am using java to write Appium test script & now i want to compare two emails, but before comparison i have to fetch the email id from a text by splitting the string.
Ex: i have text like this in my application "your account email associated with pankaj#gmail.com" so i want split & capture this email id only from this text & compare it with other email id which is showing in a text box.
how can i do this ??
Currently i am doing it like this:
WebElement email_id= driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIATextField[1]"));
String edit_email=email_id.getText();
System.out.println(edit_email);
But getting the Full text.How can i split it.
You should try regular expression using java.util.regex.Pattern and java.util.regex.Matcher. I have prepared a snippet that finds email ids from the given chunk of text.
String text = "your account email associated with pankaj#gmail.com and he has emailed someone#gmail.com.";
Pattern pattern = Pattern.compile("[\\w]+[\\d\\w]*(#)[\\w]+[\\w\\d]*(\\.)[\\w]+");
Matcher matcher = pattern.matcher(text);
while(matcher.find()){
System.out.println(matcher.group());
}
This should help.
This is doing the trick for me:
String s = "your account email associated with pankaj#gmail.com";
s = s.replaceAll("^.+\\s", "");
System.out.println(s);
If you are sure that the text that you are attempting to split is of standard format (or some static content with different email Ids), you can use regular expressions to parse and retrieve the email addresses as Nitheesh Shah and dotvav mentioned in their answers.
Otherwise, you have to follow couple of RFCs as mentioned in the below thread to perfectly retrieve and validate the email addresses (Refer the best answer which is shown at the top of the below thread).
Using a regular expression to validate an email address
As OP has mentioned it will end with email id only , another solution can be this:
WebElement email_id= driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIATextField[1]"));
String s[] = email_id.getText().split(" ");
System.out.println(s[s.length-1]);
After getting email id you can compare it with another email in textbox.
Currently I am using this & it's working for me perfectly.implemented the solution as finding the particular email substring in the main string.
String word = edit_email;
String com_txt= email_text; //Edit page Static string
Boolean same_txt =com_txt.contains(word);
Boolean result=same_txt;
if(result==true)
{
System.out.println(result);
System.out.println("Edit screen & enter email screen contains the same email");
}
Is this the right way to perform the comparison??

Java - Regex String replacement

I want to replace some parts of a String with Regex. It's the 192001Z part of the string I want to replace.
Code:
String met = "192001Z 17006KT 150V210 CAVOK 11/07 Q1004 NOSIG";
String regexZ = "[0-9].{5}Z";
met = met.replaceAll(regexZ, "${.now?string(\"ddHHmm\")}Z");
I get an error when I want to replace a part of the String with ${.now?string(\"ddHHmm\")}Z.
But when I e.g. replace ${.now?string(\"ddHHmm\")}Z with ThisNeedsToBeReplaced everything works just fine. So my guess is that something is wrong with the string I want to use to replace parts of my original string (met).
The error I receive is Illegal group reference.
Does anyone have an idea what's wrong with ${.now?string(\"ddHHmm\")}Z?
You need to use:
met = met.replaceAll("\\b\\d{6}Z\\b", "\\${.now?string(\"ddHHmm\")}Z");
Correct regex to match 192001Z is \b\d{6}Z\b
You need to escape $ in replacement as well otherwise it is considered a back reference e.g. $1, $2 etx.

how to extract unknown names out of a string

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

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

Categories

Resources