Open In App

How to Extract Domain Name From Email Address using Java?

Last Updated : 16 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given some Strings as Email addresses, the task is to extract the domain name from it

Examples:

Input: test_str = ‘team@geeksforgeeks.org’

Output: geeksforgeeks.org

Explanation: Domain name, geeksforgeeks.org extracted.

Input: test_str = ‘abcd@gmail.com’

Output: gmail.com

Explanation: Domain name, gmail.com extracted.

Approach

Method 1: Using the String split() method

This problem can be solved using the Java String split method.

Syntax: Public String [] split (String regex)

Where, regex: a delimiting regular expression

Returns: An array of strings is computed by splitting the given string.

Exception Thrown: PatternSyntaxException – if the provided regular expression’s syntax is invalid.

Below is the code implementation of the above-discussed approach:

Java




public class GFG {
    static String splitDomainName(String str,
                                  String delimeter)
    {
        String finalOutput = "";
        String arrayOfStr[] = str.split(delimeter);
        if (arrayOfStr.length == 2) {
            finalOutput = arrayOfStr[1];
        }
        return finalOutput;
    }
  
    public static void main(String args[])
    {
        //  Enter the Email as  String
        String str = "team@geeksforgeeks.org";
        
        // deleimeter
        String deleimeter = "@";
        System.out.println("The original string is :"
                           + str);
        System.out.println(
            "The extracted domain name :"
            + splitDomainName(str, deleimeter));
    }
}


Output

The original string is :team@geeksforgeeks.org
The extracted domain name :geeksforgeeks.org

Method 2: Using substring() Method of String Class

Syntax: public String substring(int beginningIndex)

          Where, beginningIndex: Starting index of the substring

Return Type: String

Note: If endIndex is not given as a parameter then Str.length()-1 will be the end string

Below is the code implementation of the above-discussed approach:

Java




public class GFG {
    static String str1 = "";
    static String getEmailDomain(String str)
    {
        str1 = str.substring(str.indexOf("@") + 1);
        return str1;
    }
  
    public static void main(String args[])
    {
        String str = "team@geeksforgeeks.org";
        System.out.println("The original string is :"
                           + str);
        System.out.println("The extracted domain name :"
                           + getEmailDomain(str));
    }
}


Output

The original string is :team@geeksforgeeks.org
The extracted domain name :geeksforgeeks.org

Method 3: Using StringUtils.substringAfter() from Apache Commons Lang

StringUtils is a class that is used to handle String and operations performed in the string, it provides more utility methods than the Java String class. This class does not belong to the Java package; instead, it belongs to the Apache Commons Library.

import org.apache.commons.lang3.StringUtils;

Write it in the written class source code to use all the functionality. Add the following dependency for Apache Commons Library in the maven project pom.xml file 

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>3.12.0</version>
</dependency>

Below is the code implementation of the above-discussed approach:

Java




import org.apache.commons.lang3.StringUtils;
  
public class GFG {
    static String str1 = "";
    static String getDomainNameEmail(String str)
    {
        str1 = StringUtils.substringAfter(str, "@");
        return str1;
    }
  
    public static void main(String args[])
    {
  
        String str = "team@geeksforgeeks.org";
        System.out.println("The original string is :"
                           + str);
        System.out.println("The extracted domain name :"
                           + getDomainNameEmail(str));
    }
}
  
// This online compiler does not support StringUtils
// for running this program ,Its dependency must be put into
// the
// maven project pom.xml file


Output:

The original string is :team@geeksforgeeks.org
The extracted domain name :geeksforgeeks.org


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads