org.apache.commons常用类的使用

/**
     * 文本相似度计算
     */
	public static void similarity() {
        JaccardSimilarity jaccardSimilarity = new JaccardSimilarity();
        String str1 = "网站拒绝重复写信功能,";
        String str2 = "网站拒绝重复写信功能";
        double value = jaccardSimilarity.apply(str1, str2);
        System.out.println("相似度=" + value + "\n");
    }
	
	/**
	 * 随机字符串生成
	 */
	public static void generateRandomString(){
		RandomStringGenerator.Builder builder = new RandomStringGenerator.Builder();
		RandomStringGenerator generator = null;
		String s = "";
		
		//使用字母 a-z
		generator = builder.withinRange('a', 'z').build();
        s = generator.generate(20);
        System.out.println(StringUtils.center("随机字母字符串", 20, "="));
        System.out.println(s);
        
        //使用数字 0-9
        generator = builder.withinRange('0', '9').build();
        s = generator.generate(20);
        System.out.println(StringUtils.center("随机数字字符串", 20, "="));
        System.out.println(s);
        
        //使用字符 0-z
        generator = builder.withinRange('0', 'z').build();
        s = generator.generate(20);
        System.out.println(StringUtils.center("随机混合字符串", 20, "="));
        System.out.println(s + "\n");
	}
	
	/**
	 * 随机字符串
	 */
	public static void randomString(){
        System.out.println(RandomStringUtils.randomAlphabetic(5)); //大小写字母组合
        System.out.println(RandomStringUtils.randomAlphanumeric(5)); //大小写字母、数字的组合
        System.out.println(RandomStringUtils.randomAscii(5)); //Ascii码
        System.out.println(RandomStringUtils.randomNumeric(5)); //随机数字
	}
	
	/**
	 * 占位符替换
	 */
	public static void strSubstitutor(){
		Map<String, Object> params = new HashMap<String, Object>();
        params.put("user", "admin");
        params.put("password", "123456");
        params.put("system-version", "windows 10");
        params.put("版本", "version");

		//StrSubstitutor不是线程安全的类
        StrSubstitutor strSubstitutor = new StrSubstitutor(params, "${", "}");
        //是否在变量名称中进行替换
        strSubstitutor.setEnableSubstitutionInVariables(true);
        String s = strSubstitutor.replace("你的用户名是${user},密码是${password}。系统版本${system-${版本}}");
        System.out.println(s);
	}

 

相似度=0.91

 

======随机字母字符串=======

gbxhdukdfemasxpiohmc

======随机数字字符串=======

71835702082200695740

======随机混合字符串=======

9L4GnpSaS9TGDB;rXzFq

 

lyEfn

tdtBf

>;#^l

48314

 

你的用户名是admin,密码是123456。系统版本windows 10

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值