I want to split an email string in java (android) but it not work correctly.
Input: "ihnel48@gmail.com"
String[] pattens = email.split("@.");
Expected: "ihnel48", "gmail", "com"
Output: "ihnel48" "mail.com"
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because
String.splitmatches based on a regular expression,@.means it looks for two characters in a row (not either character once). And,.in regular expressions is a special character meaning “anything”:In your case this matches “@g” and not the dot.
Instead, you want:
The square brackets,
[], create a character class, which represents all the valid characters a single position can match. So, you need to match “@” or “.“. The character.does not need to be escaped inside a character class.