在一个字符串中截取指定字符串,域名截取,尝试截取不同网址的域名?

/**
 *    在一个字符串中截取指定字符串,域名截取,尝试截取不同网址的域名?
 *比如www.163.com,www.sohu.com.cn
 *     字符串截取就需要用subString()
 *     索引的位置,这里需要找第一个"."作为每次域名的开始索引,然后找下一个("."+1)作为结束位置
 *     第一个点好找,indexOf搞定,第二点,用indexOf(".",之前的"."+1)找到
 */
public class StringDemo6 {
    public static void main(String[] args) {
        String http = "www.163.com";
        String http2 = "www.sohu.com.cn";
        int index1 = http2.indexOf(".");
        int index2 = http2.indexOf(".",index1+1);
        String domainName = http2 .substring(index1+1,index2);
        System.out.println(domainName);

    }
}
View Code

/**
* 在一个字符串中截取指定字符串,域名截取,尝试截取不同网址的域名?
*比如www.163.com,www.sohu.com.cn
* 字符串截取就需要用subString()
* 索引的位置,这里需要找第一个"."作为每次域名的开始索引,然后找下一个("."+1)作为结束位置
* 第一个点好找,indexOf搞定,第二点,用indexOf(".",之前的"."+1)找到
*/

posted @ 2016-05-01 11:25  安仔80  阅读(1032)  评论(0编辑  收藏  举报