I have a string that saves user login name and I want to remove specific characters from that string,i want to remove "#gmail.com" and just have the name before the #, then save it as a new string?
How can I do this?
Here's an example, email can be any email address, not just gmail.com
public class Test {
public static void main(String[] args) {
String email = "nobody#gmail.com";
String nameOnly = email.substring(0,email.indexOf('#'));
System.out.println(nameOnly);
}
}
make sure the email format be correct then use "split" method to split the string from '#' character's position and use first portion of results.
var str = "username#amailserver.com";
var res = str.split("#");
var username = res[0];
You can use regex + replaceAll method of string for eliminate it
sample:
String s = "Rod_Algonquin#company.co.nz";
String newS = s.replaceAll("#(.*).(.*)", "");
System.out.println(newS);
will work on different sites extension.
if you want .org, .net , etc then you need to change the regex #(.*).(.*)
Related
I have a piece of data in the following formats/patterns :
String inputFruit = "[Apple,Banana(Mango-Juice,lemon-Pickle,Grape-Drinks)]";
String inputFruit = "Apple,Banana(Mango-Juice,lemon-Pickle,Grape-Drinks)"
String inputFruit = "Apple(Mango-Juice,lemon-Pickle,Grape-Drinks)Banana"
Now I have to extract and store individual datas like :
firstFruit = Apple
secondFruit = Banana
miscFruit = Mango-Juice,lemon-Pickle,Grape-Drinks
I have the following code snippet which I am using :
public static void splitFruits(String inputFruit)
{
String firstFruit = StringUtils.EMPTY;
String secondFruit = StringUtils.EMPTY;
String miscFruit = StringUtils.EMPTY;
inputFruit = inputFruit.replaceAll("\\[" , "");
inputFruit = inputFruit.replaceAll("\\]" , "");
String frts[] = inputFruit.split("\\("");
String frtp[] = frts[0].split(",");
firstFruit = frtp[0];
secondFruit = frtp[1];
miscFruit = frts[1];
}
Here I need to store Apple in variable firstFruit, Banana in secondFruit, and whatever is there inside () in miscFruit.
My code is able to extract value for a specific patter mentioned in no 1.How can I create pattern match statements to match with input values in all the above specified 3 different formats and store them separately.
Instead of using frts[0] to get the first and second fruits, combine frts[0] and frts[2] (that is, the parts on either side of the parenthetical section) and split that.
I know there are similar questions regarding to this. However, I tried many solutions and it just does not work for me.
I need help to extract multiple substrings from a string:
String content = "Ben Conan General Manager 90010021 benconan#gmail.com";
Note: The content in the String may not be always in this format, it may be all jumbled up.
I want to extract the phone number and email like below:
1. 90010021
2. benconan#gmail.com
In my project, I was trying to get this result and then display it into 2 different EditText.
I have tried using pattern and matcher class but it did not work.
I can provide my codes here if requested, please help me ~
--------------------EDIT---------------------
Below is my current method which only take out the email address:
private static final String EMAIL_PATTERN =
"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
"\\#" +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
"(" +
"\\." +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
")+";
public String EmailValidator(String email) {
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
if (matcher.find()) {
return email.substring(matcher.start(), matcher.end());
} else {
// TODO handle condition when input doesn't have an email address
}
return email;
}
You can separate your string into arraylist like this
String str = "Ben Conan, General Manager, 90010021, benconan#gmail.com";
List<String> List = Arrays.asList(str.split(" "));
maybe you should do this instead of yours :
String[] Stringnames = new String[5]
Stringnames [0] = "your phonenumber"
Stringnames[1] = "your email"
System.out.println(stringnames)
Or :
String[] Stringnames = new String[2]
String[] Stringnames = {"yournumber","your phonenumber"};
System.out.println(stringnames [1]);
String.split(...) is a java method for that.
EXAMPLE:
String content = "Ben Conan, General Manager, 90010021, benconan#gmail.com";
String[] selection = content.split(",");
System.out.println(selection[0]);
System.out.println(selection[3]);
BUT if you want to do a Regex then take a look at this:
https://stackoverflow.com/a/16053961/982161
Try this regex for phone number
[\d+]{8} ---> 8 represents number of digits in phone number
You can use
[\d+]{8,} ---> if you want the number of more than 8 digits
Use appropriate JAVA functions for matching. You can try the results here
http://regexr.com/
For email, it depends whether the format is simple or complicated. There is a good explanation here
http://www.regular-expressions.info/index.html
I simply want to replace all words starting with "http" and ends with space or "\n" in a string
Example string is.
Full results below;
http://www.google.com/abc.jpg is a url of an image.
or some time it comes like https://www.youtube.com/watch?v=9Xwhatever this is an example text
Result of the string should be like
is a url of an image.
or some time it comes like this is an example text
I simply want to replace it with ""; i know the logic but don't know the function.
My logic is
string.startwith("http","\n")// starts with http and ends on next line or space
.replaceAll("")
public static void main(String[] args) {
String s = "https://www.google.com/abc.jpg is a url of an image.";
System.out.println(s.replaceAll("https?://.*?\\s+", ""));
}
O/P :
is a url of an image.
String.replaceAll() allows you to use a regex. In a regex, ^ allows you to capture the beginning of the String. Hence, you can do like that :
System.out.print("http://google-http".replaceAll("^http", ""));
result:
://google-http
The http at the beginning has be removed but not the one at the end.
public static void main(String[] args) {
String str = "https://www.google.com/abc.jpg is a url of an image.";
String subStr1 = "http://";
String substr2 = "https://";
String foundStr = "";
if(str.startsWith(subStr1)) {
foundStr = subStr1;
}
if (str.startsWith(subStr2)) {
foundStr = subStr2;
}
str = str.replaceAll(foundStr, "");
str = str.replaceAll(" ", "");
}
I have a string like
String email = "mailto://abc#gmail.com";
I want to get only the email address but without using a fixed number like
email.substring(9);
Any better approach.
The String is of the URI format so you could do
String email = "mailto://abc#gmail.com";
URI uri = URI.create(email);
String address = uri.getUserInfo() + "#" + uri.getHost();
Use a regular expression:
String email = "mailto://abc#gmail.com";
// Builds a pattern with a capturing group ()
Pattern mailtoPattern = Pattern.compile("mailto://(.*)");
// Give your string to a matcher generated by the compiled pattern
Matcher mailMatcher = mailtoPattern.matcher(email);
// If your String is correctly formatted you can attempt to capture
// the content between parenthesis
if (mailMatcher.find()) {
String mailValue = emailMatcher.group(1);
}
Using regular expressions will also help you validate the String given as input, you can even validate if the mail String is indeed a mail address (there are crazy people with all sorts of crazy expressions to validate them). I recommend you read the very thorough JavaDoc here: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html.
Not using regex
String string = "mailto://abc#gmail.com";
final String PREFIX = "mailto://";
String email = string.substring(PREFIX.length());
Found lots of examples on string.replaceall but I cannot figure out how to use regex to solve my problem. I am looking to find and replace all occurrences of the string [reset_token] within my message.
Code I have so far:
String message = "Your new token is [reset_token]";
String newbody = replaceDelimiter("^[reset_token]", "mynewtoken");
public String replaceDelimiter(String delimiter, String message) {
return message.replaceAll(delimiter, message);
}
I would like the result to be "Your new token is mynewtoken"
You don't need replaceAll here, as your pattern is not really a regex, but is static. Simple replace would work fine:
String newbody = message.replace("[reset_token]", "mynewtoken");
And also you don't need that extra method wrapping your replace call.
You need replace() instead as it replaces the string token as is:
String message = "Your new token is [reset_token]";
String newbody = message.replace("[reset_token]", "mynewtoken");