How to check if the string is valid Url path format

Multi tool use
Multi tool use


How to check if the string is valid Url path format



Understands that using Patterns.WEB_URL.matcher(url).matches() will able to validate if that string is a valid url, however it will required in a full format which contain https: / .com.



In my case, i want to validate if the json return a string in correct format for path e.g /images/slider/my/myImage.jpg; which does not contain https or any. How can i do this?



What i want to do is something like:


if(ImageUrl equal "/images/slider/my/myImage.jpg" FORMAT) {
//Do something here
} else //ImageUrl = myImage.jpg {
//Add "/images/slider/my/" infront of the text
}



P.s: My image link will be like www.abc.com/images/slider/my/myImage.jpg




2 Answers
2



U can use matcher if the string contain /images/slider/my/


public static boolean isValidImage(String imageUrl){
boolean isValid = false;
Pattern pattern = Pattern.compile("/images/slider/my");
Matcher matcher = pattern.matcher(imageUrl);
if (matcher.find()) {
isValid = true;
}
return isValid;
}



Then in your main function


if(isValidImage(imageUrl) {
//Do something here
} else {
//Add "/images/slider/my/" infront of the text
}



Use URLUtil to validate the URL as below.


URLUtil.isValidUrl(url)



It will return True if URL is valid and false if URL is invalid.



Another way is given below.


public static boolean checkURL(CharSequence input) {
if (TextUtils.isEmpty(input)) {
return false;
}
Pattern URL_PATTERN = Patterns.WEB_URL;
boolean isURL = URL_PATTERN.matcher(input).matches();
if (!isURL) {
String urlString = input + "";
if (URLUtil.isNetworkUrl(urlString)) {
try {
new URL(urlString);
isURL = true;
} catch (Exception e) {
}
}
}
return isURL;
}



This link will explain how you can check the url is available or not.



For more about URLS please visit this



Please have a try





Both method still return false for "/images/slider/my/myImage.jpg". Which means i will need to add Http:// infont for validate
– Shawn.Y
Jul 3 at 6:08





Yes . without http it is not a valid url
– Pranav MS
Jul 3 at 7:16





how could i do if i want to check if the image url path is reachable? I means instead of checking the string only i want to test if the url can run
– Shawn.Y
Jul 3 at 9:05






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Dt985 fZkcqOkzQBmCieQSywR4iqSC LiEbK2PkwJbq,Sxww,q6,unU 7bDczWRN JMANKn,s2m
mxmi TEmArl2v1,ntz9w8V5C8q5WLXgcBZz,3CTmE,YKWe 8iFL,VgPX Y,yv,1,Ghu3,hLi4Yg5xdvF0ogKj

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications