常用工具类2

10. Formatter类的简单使用

例1. Formatter简单输出示例。

package util;

import java.util.*;

public class demoFormatter {



    public static void main(String[] args) {

        //以标准输出设备为目标,创建对象

        Formatter fmt = new Formatter(System.out);

        //格式化输出数据,并输出到标准输出设备

        fmt.format("直接输出,每个输出项占8个字符位:%8d%8d\n",100,200);

        StringBuffer buf = new StringBuffer();

        //以指定的字符串为目标,创建对象

        fmt = new Formatter(buf);

        //格式化输出数据,输出到buf中

        fmt.format("输出到指定的缓冲区,每个输出项占6个字符位:%6d%6d\n", 300,400);

        //再从buf中输出到屏幕

        System.out.print(buf);

        //以默认的存储区为目标,创建对象

        fmt = new Formatter();

        //格式化输出数据,输出到自己的缓冲区

        fmt.format("输出到自己的缓冲区,每个输出项占10个字符位:%10.3f%10.3f", 123.45,43.687);

        //再从对象的存储区中输出到屏幕

        System.out.println(fmt);

    }



}

程序运行结果如下:

直接输出,每个输出项占8个字符位:     100     200

输出到指定的缓冲区,每个输出项占6个字符位:   300   400

输出到自己的缓冲区,每个输出项占10个字符位:   123.450    43.687

11. 时间格式转换符详解

例2. 使用时间格式转换符输出日期和时间。

package util;

import java.util.*;

public class demoFmtTime {



    public static void main(String[] args) {

        Formatter fmt = new Formatter(System.out);

        Date dt = new Date();

        fmt.format("现在的日期和时间(以默认的完整格式):%tc\n", dt);

        fmt.format("今天的日期(按中国习惯):%1$tY-%1$tm-%1$td\n", dt);

        fmt.format("今天是:%tA\n", dt);

        fmt.format("现在的时间(24小时制):%tT\n", dt);

        fmt.format("现在的时间(12小时制):%tr\n", dt);

        fmt.format("现在是:%tH点%1$tM分%1$tS秒", dt);

    }



}

程序运行结果如下:

现在的日期和时间(以默认的完整格式):星期六 一月 24 14:25:35 CST 2015

今天的日期(按中国习惯):2015-01-24

今天是:星期六

现在的时间(24小时制):14:25:35

现在的时间(12小时制):02:25:35 下午

现在是:14点25分35秒

12. Pattern类的使用

例3. Pattern使用示例。

package util;



import java.util.regex.Pattern;



public class demoPattern_1 {

    static String text ="Kevin has seen 《LEON》several times, because it is a good film."+"/凯文已经看过《这个杀手不太冷》几次了,因为它是一部好电影。/名词:凯文。";

    public static void main(String[] args) {

        Pattern p = Pattern.compile("[/]+");

        String[] result = p.split(text);

        for(int i=0;i<result.length;i++)

            System.out.println(result[i]);

    }



}

程序运行结果如下:

Kevin has seen 《LEON》several times, because it is a good film.

凯文已经看过《这个杀手不太冷》几次了,因为它是一部好电影。

名词:凯文。

指定分段的段数:

package util;



import java.util.regex.Pattern;



public class demoPattern_2 {

    static String text ="Kevin has seen 《LEON》several times, because it is a good film."+"/凯文已经看过《这个杀手不太冷》几次了,因为它是一部好电影。/名词:凯文。";

    public static void main(String[] args) {

        Pattern p = Pattern.compile("[/]+");

        String[] result = p.split(text,2);

        for(int i=0;i<result.length;i++)

            System.out.println(result[i]);

    }

}

程序运行结果如下:

Kevin has seen 《LEON》several times, because it is a good film.

凯文已经看过《这个杀手不太冷》几次了,因为它是一部好电影。/名词:凯文。

13. Matcher类的使用

Matcher类需要配合Pattern使用。他没有提供构造方法,因为必须用Pattern对象来创建Matcher对象。

例4. 匹配方法使用示例。

package util;



import java.util.regex.Matcher;

import java.util.regex.Pattern;



public class demoMatching {

    static String text = "This is a test string.";

    public static void main(String[] args) {

        Pattern p = Pattern.compile("\\bTh");

        Matcher m = p.matcher(text);

        System.out.println("正文串:"+text);

        System.out.println("表达式:"+"\\bTh");

        System.out.println("整个正文串的匹配结果:"+m.matches());

        System.out.println("字串匹配结果:"+m.find());

        System.out.println("匹配正文串的起始部分:"+m.lookingAt());



    }



}

程序运行结果:

正文串:This is a test string.

表达式:\bTh

整个正文串的匹配结果:false

字串匹配结果:true

匹配正文串的起始部分:true

14. 替换方法的使用

例5. 将把句子里的”Kelvin“改为"Kevin"。

package util;



import java.util.regex.Matcher;

import java.util.regex.Pattern;



public class demoReplace {



    public static void main(String[] args) {

        String text = "Kelvin Li and Kelvin Chan are both working in Kelvin"+

    "Chen's KelvinSoftShop company";

        Pattern p = Pattern.compile("Kelvin");

        Matcher m = p.matcher(text);

        StringBuffer sb = new StringBuffer();

        int cnt = 0;

        boolean result;

        while(m.find()){

            m.appendReplacement(sb, "Kevin");

            cnt++;

            System.out.println("第"+cnt+"次匹配sb的内容是:"+sb);

        }

        m.appendTail(sb);

        System.out.println("调用m.appendTail(sb)后sb的最终内容是:"+sb);

    }



}

程序运行结果如下:

第1次匹配sb的内容是:Kevin

第2次匹配sb的内容是:Kevin Li and Kevin

第3次匹配sb的内容是:Kevin Li and Kevin Chan are both working in Kevin

第4次匹配sb的内容是:Kevin Li and Kevin Chan are both working in KevinChen's Kevin

调用m.appendTail(sb)后sb的最终内容是:Kevin Li and Kevin Chan are both working in KevinChen's KevinSoftShop company

更为简洁的方法:

import java.util.regex.Pattern;



public class demoReplace {



    public static void main(String[] args) {

        String text = "Kelvin Li and Kelvin Chan are both working in Kelvin"+

    "Chen's KelvinSoftShop company";

        Pattern p = Pattern.compile("Kelvin");

        Matcher m = p.matcher(text);

        text = m.replaceAll("Kevin");

        System.out.println("替换后为:"+text);

//        StringBuffer sb = new StringBuffer();

//        int cnt = 0;

//        boolean result;

//        while(m.find()){

//            m.appendReplacement(sb, "Kevin");

//            cnt++;

//            System.out.println("第"+cnt+"次匹配sb的内容是:"+sb);

//        }

//        m.appendTail(sb);

//        System.out.println("调用m.appendTail(sb)后sb的最终内容是:"+sb);

    }



}

输出结果为:

替换后为:Kevin Li and Kevin Chan are both working in KevinChen's KevinSoftShop company

15. 组匹配的使用

例6. 组匹配使用示例。

import java.util.regex.Pattern;



public class demoGroup {



    public static void main(String[] args) {

        String text = "REP_0_12_4567";

        String rex = "(REP_(\\d{1})_(\\d{1,2})(_(\\d{1}))?)";

        String result[];

        Pattern pTest = Pattern.compile(rex);

        Matcher m = pTest.matcher(text);

        if(m.find()){

            int cnt = m.groupCount(); //获取匹配组数目

            result = new String[cnt+1];

            for(int i=1;i<=cnt;i++){

                result[i]=m.group(i);

                System.out.println("第"+i+"组:"+result[i]);

            }

        }else{

            System.out.println("匹配不成功");

        }



    }



}

程序结果为:

第1组:REP_0_12_4

第2组:0

第3组:12

第4组:_4

第5组:4

16. 检验Email的合法性

例7. 利用正则表达式检验Email的合法性。

package util;



import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;



public class checkEmail {



    public static void main(String[] args) {

        // 命令的第一个参数是为要检测的Email地址

//        String input = args[0];

        Scanner in = new Scanner(System.in);

        System.out.println("请输入Email地址:");

        String input = in.nextLine();

        in.close();

        // 检测输入的Email地址是否以非法符号"."或"@"作为起始字符

        Pattern p = Pattern.compile("^\\.|^\\@");

        Matcher m = p.matcher(input);

        if(m.find())

            System.out.println("Email地址不能以'.'或‘@'作为起始字符");

        //检测是否以"www."为起始

        p = Pattern.compile("^www\\.");

        m = p.matcher(input);

        if(m.find())

            System.out.println("Email地址不能以’www.‘起始");

        //检测是否包含非法字符

        p = Pattern.compile("[^A-Za-z0-9\\.\\@_\\-~#]+");

        m = p.matcher(input);

        StringBuffer sb = new StringBuffer();

        boolean result = m.find();

        boolean deletedIllegalChars = false;

        while(result){

            //如果找到了非法字符,那么就设下标记

            deletedIllegalChars = true;

            //如果里面包含非法字符,如冒号双引号等,那么就把它们消去,加到sb里面

            m.appendReplacement(sb, "");

            result = m.find();

        }

        m.appendTail(sb);

        String input1 = sb.toString();

        if(deletedIllegalChars){

            System.out.println("输入的Email地址里包含有冒号、逗号等非法字符,请修改");

    //        System.out.println("您现在的输入为:"+args[0]);

            System.out.println("您现在的输入为:"+input);

            System.out.println("修改后合法的地址应类似:"+input1);

        }

    }



}

程序运行结果为:

请输入Email地址:

myEmail$:@gmail.com

输入的Email地址里包含有冒号、逗号等非法字符,请修改

您现在的输入为:myEmail$:@gmail.com

修改后合法的地址应类似:[email protected]
请输入Email地址:

[email protected]

Email地址不能以’www.‘起始
请输入Email地址:

guanlei@123.com

 

你可能感兴趣的:(工具类)