java_2_位置切割

待解析字符串

String url="http://120.132.0.117:8080/yun90/article/GetRelatedArticles";

代码

package com.basic;

public class Parse02 {
    public static void main(String[] args) {
        String post = "POST http://120.132.0.117:8080/yun90/article/GetRelatedArticles HTTP/1.1\r\nHost: blog.csdn.net\r\nConnection: keep-alive\r\n\r\npageindex=2&articleId=23462041";
        String url="http://120.132.0.117:8080/yun90/article/GetRelatedArticles";
        
        /*
         * 一、按位置切割请求协议:即http
         */
        int index=url.indexOf("://");
        String scheme=url.substring(0,index);
        System.out.println("scheme="+scheme);
        url=url.substring(index+3);
        System.out.println("new url="+url+"\r\n");
        
        /*
         * 二、解析ip
         */
        index=url.indexOf(":");
        String ip=url.substring(0,index);
        System.out.println("ip="+ip);
        url=url.substring(index+3);
        System.out.println("new url="+url+"\r\n");

        
        /*
         * 三、解析端口
         */
        index=url.indexOf("/");
        String port=url.substring(0,index);
        System.out.println("port="+port);
        url=url.substring(index+3);
        System.out.println("new url="+url+"\r\n");

        /*
         * 四、解析应用名和资源路径
         */
        index=url.indexOf("/");
        String context=url.substring(0,index);
        System.out.println("context="+context);
        String urlPattern=url.substring(index+3);
        System.out.println("urlPattern="+urlPattern);
        
    }
}

你可能感兴趣的:(java_2_位置切割)