Skip to content

Commit ebc64cd

Browse files
Added 4 useful methods
/** * Returns the domain part of a given Email address * * @param email The Email address. E.g Returns "protonmail.com" from the given Email "johnsmith@protonmail.com". * @return the domain part of a given Email address. */ public static String extractEmailProvider(String email) { return email.substring(email.lastIndexOf("@") + 1); } /** * Returns the username part of a given Email address. E.g. Returns "johnsmith" from the given Email "johnsmith@protonmail.com". * * @param email The Email address. * @return the username part of a given Email address. */ public static String extractEmailUsername(String email) { return email.substring(0, email.lastIndexOf("@")); } /** * Return whether a given Email address is on a specified Email provider. E.g. "johnsmith@protonmail.com" and "gmail.com" will return false. * * @param email The Email address. * @param emailProvider The Email provider to testify against. * @return {@code true}: yes<br>{@code false}: no */ public static boolean isFromEmailProvider(String email, String emailProvider) { return extractEmailProvider(email).equalsIgnoreCase(emailProvider); } /** * Return whether a given Email address is on any of the specified Email providers list (array). E.g. Useful if you pass it a list of real Email provider services and check if the Email is a disposable Email or a real one. * * @param email The Email address. * @param emailProviders The list of Email providers to testify against. * @return {@code true}: yes<br>{@code false}: no */ public static boolean isFromAnyOfEmailProviders(String email, String[] emailProviders) { return com.blankj.utilcode.util.ArrayUtils.contains(emailProviders, extractEmailProvider(email)); }
1 parent 66a4c04 commit ebc64cd

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

lib/utilcode/src/main/java/com/blankj/utilcode/util/RegexUtils.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,54 @@ public static boolean isMobileSimple(final CharSequence input) {
4040
return isMatch(RegexConstants.REGEX_MOBILE_SIMPLE, input);
4141
}
4242

43+
44+
/**
45+
* Returns the domain part of a given Email address
46+
*
47+
* @param email The Email address. E.g Returns "protonmail.com" from the given Email "johnsmith@protonmail.com".
48+
* @return the domain part of a given Email address.
49+
*/
50+
public static String extractEmailProvider(String email) {
51+
return email.substring(email.lastIndexOf("@") + 1);
52+
}
53+
54+
/**
55+
* Returns the username part of a given Email address. E.g. Returns "johnsmith" from the given Email "johnsmith@protonmail.com".
56+
*
57+
* @param email The Email address.
58+
* @return the username part of a given Email address.
59+
*/
60+
public static String extractEmailUsername(String email) {
61+
return email.substring(0, email.lastIndexOf("@"));
62+
}
63+
64+
65+
/**
66+
* Return whether a given Email address is on a specified Email provider. E.g. "johnsmith@protonmail.com" and "gmail.com" will return false.
67+
*
68+
* @param email The Email address.
69+
* @param emailProvider The Email provider to testify against.
70+
* @return {@code true}: yes<br>{@code false}: no
71+
*/
72+
public static boolean isFromEmailProvider(String email, String emailProvider) {
73+
return extractEmailProvider(email).equalsIgnoreCase(emailProvider);
74+
}
75+
76+
/**
77+
* Return whether a given Email address is on any of the specified Email providers list (array). E.g. Useful if you pass it a list of real Email provider services and check if the Email is a disposable Email or a real one.
78+
*
79+
* @param email The Email address.
80+
* @param emailProviders The list of Email providers to testify against.
81+
* @return {@code true}: yes<br>{@code false}: no
82+
*/
83+
public static boolean isFromAnyOfEmailProviders(String email, String[] emailProviders) {
84+
return com.blankj.utilcode.util.ArrayUtils.contains(emailProviders, extractEmailProvider(email));
85+
}
86+
87+
88+
89+
90+
4391
/**
4492
* Return whether input matches regex of exact mobile.
4593
*

0 commit comments

Comments
 (0)