Java 正则表达式 之 分割

http://www.verejava.com/?id=16994950342031

package com.reg;

public class TestSplit
{
    public static void main(String[] args)
    {
        testContry();
    }
    
    /**
     * 4.    将下面的国家分割打印
            ChinaqqqAmericahhhhhEnglandaaaaaaMexica
     */
    public static void testContry()
    {
        String country="ChinaqqqqqAmericahhhEnglandaaaaaaMexica";
        String[] countrys=country.split("(.)\\1+");
        for(String str:countrys)
        {
            System.out.println(str);
        }
    }
    
    
    /**
     * 3.    将下面小学生的成绩分割打印出来
            数学=100,语文=69;英语=70,计算机=90:地理=85,体育=50
     */
    public static void testScore()
    {
        String score="数学=100,语文=69;英语=70,计算机=90:地理=85,体育=50";
        String[] scores=score.split("[,;:]");
        for(String str:scores)
        {
            System.out.println(str);
        }
    }
    
    /**
     * String str=”薪水 职位 姓名 年龄 性别”;
     */
    public static void testSpace()
    {
        /*String info="薪水  职位      姓名      年龄 性别";
        String[] infoArray=info.split(" +");*/
        String info="薪水      职位      姓名 年龄 性别";
        String[] infoArray=info.split("[ \t]+");
        for(String str:infoArray)
        {
            System.out.println(str);
        }
    }
    
    /**
     * 1.    以点号 . 切割IP 地址判断是否为C类IP地址
            C类IP地址:192.168.0.0~192.168.255.255
     */
    public static void testCIP(String ip)
    {
        String[] ips=ip.split("\\.");
        for(String str:ips)
        {
            System.out.println(str);
        }
    }
}

http://www.verejava.com/?id=16994950342031

评论

暂无评论