Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中检测字符串是否包含URL?_Java_Android_String_Token_Tokenize - Fatal编程技术网

如何在java中检测字符串是否包含URL?

如何在java中检测字符串是否包含URL?,java,android,string,token,tokenize,Java,Android,String,Token,Tokenize,可能重复: 假设我有字符串: “我喜欢访问和访问www.apple.com” 如何标记此字符串,并确定该标记是否包含URL?请参阅: import java.net.URL; 导入java.net.MalformedURLException; //用html hrefs代码替换URL 公开课{ 公共静态void main(字符串[]args){ 字符串s=args[0]; //按空格分隔输入(URL没有空格) 字符串[]部分=s.split(\\s”); //尝试将每个项目转换为URL。 对于

可能重复:

假设我有字符串:

“我喜欢访问和访问www.apple.com”

如何标记此字符串,并确定该标记是否包含URL?

请参阅:

import java.net.URL;
导入java.net.MalformedURLException;
//用html hrefs代码替换URL
公开课{
公共静态void main(字符串[]args){
字符串s=args[0];
//按空格分隔输入(URL没有空格)
字符串[]部分=s.split(\\s”);
//尝试将每个项目转换为URL。
对于(字符串项:部件)请尝试{
URL=新的URL(项目);
//如果可能,则更换为锚定。。。
系统输出打印(“”);
}捕获(格式错误){
//如果有一个网址,那不是它!。。。
系统输出打印(项目+“”);
}
System.out.println();
}
}
从,

import java.net.URL;
import java.net.MalformedURLException;

// Replaces URLs with html hrefs codes
public class URLInString {
    public static void main(String[] args) {
        String s = args[0];
        // separete input by spaces ( URLs don't have spaces )
        String [] parts = s.split("\\s");

        // Attempt to convert each item into an URL.   
        for( String item : parts ) try {
            URL url = new URL(item);
            // If possible then replace with anchor...
            System.out.print("<a href=\"" + url + "\">"+ url + "</a> " );    
        } catch (MalformedURLException e) {
            // If there was an URL that was not it!...
            System.out.print( item + " " );
        }

        System.out.println();
    }
}