What\'s the best way to check if a String contains a URL in Java/Android? Would the best way be to check if the string contains |.com | .net | .org | .info | .everythingelse
This function is working for me
private boolean containsURL(String content){
String REGEX = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
Pattern p = Pattern.compile(REGEX,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(content);
if(m.find()) {
return true;
}
return false;
}
Call this function
boolean isContain = containsURL("Pass your string here...");
Log.d("Result", String.valueOf(isContain));
NOTE :- I have tested string containing single url