Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter a String in java with specific range (email format)

Tags:

java

string

I have a String in Java called Kiran<[email protected]>. I want to get String just [email protected] by removing other content.

String s1= kiran<[email protected]>

Output should be [email protected]

Please help me out in solving this.

like image 929
Akshay Avatar asked May 13 '12 10:05

Akshay


People also ask

How do you cut a string upto a certain character?

trim() . trim() removes spaces before the first character (which isn't a whitespace, such as letters, numbers etc.) of a string (leading spaces) and also removes spaces after the last character (trailing spaces).

How do I cut a specific string in Java?

The trim() method in Java String is a built-in function that eliminates leading and trailing spaces. The Unicode value of space character is '\u0020'. The trim() method in java checks this Unicode value before and after the string, if it exists then removes the spaces and returns the omitted string.

How to filter object in Java?

How to use filter() method in Java 8. Java 8 Stream interface introduces filter() method which can be used to filter out some elements from object collection based on a particular condition. This condition should be specified as a predicate which the filter() method accepts as an argument.

How do you delete text after a specific character in Java?

The idea is to use the deleteCharAt() method of StringBuilder class to remove first and the last character of a string. The deleteCharAt() method accepts a parameter as an index of the character you want to remove.


1 Answers

If you are trying to parse email addresses, I'd recommend to use the InternetAddress class. It is part of Java EE (if you are using Java SE you need to add the javax.mail dependency).

That class is able to parse an String containing an email address like yours.

String s1 = "kiran<[email protected]>";
InternetAddress address = new InternetAddress(s1);
String email = address.getAddress();

I think this way:

  • Your algorithm is automatically standards-compliant
  • Your code is cleaner than using regular expressions to extract the email address.
like image 130
Guido Avatar answered Oct 05 '22 12:10

Guido