java - Android - 如何验证 url 并添加 http ://www

标签 java android

我想验证网址字符串并添加 http://www如果需要的话。 该网址可能是“google.com”或“google.co.in”,因此很难在字符串末尾进行转发。

我该怎么做?

最佳答案

您可以尝试正则表达式:

public static final String URL_REGEX = "^((https?|ftp)://|(www|ftp)\\.)?[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$";

Pattern p = Pattern.compile(URL_REGEX);
Matcher m = p.matcher("example.com");//replace with string to compare
if(m.find()) {
    System.out.println("String contains URL");
}

信用:https://stackoverflow.com/a/11007981/4211264

关于java - Android - 如何验证 url 并添加 http ://www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33980684/

相关文章:

java - Jetty 不包括一些密码套件。为什么?

android - 将数据从 Android Activity 发送到 React Native

android - iptables 日志输出清理

android - 如何根据第二个微调器选择更新一个微调器

android - ListFragment 和菜单选项

java - 使用缓冲区的 ReadLine() ?

java - AWSParameterStore获取所有键和值

java - 如何在 Java 中设置全局默认日期格式?

java - 如何使用 Primefaces 时钟组件获取 UTC 格式的日期时间?

java - 我需要同步对中断方法的调用吗?