Skip to content

Commit 64555be

Browse files
authored
Merge pull request Blankj#1498 from NotNotMarshall/patch-5
Added 4 useful methods
2 parents 66a4c04 + ebc64cd commit 64555be

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 "[email protected]".
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 "[email protected]".
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. "[email protected]" 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)