Java 基础操作库 hoe 使用介绍

来源:互联网 发布:淘宝专业术语大全 编辑:程序博客网 时间:2024/06/06 08:59

hoe基本介绍

hoe 是一个 Java 基础操作库,包括 String,number,random 等常规操作,几乎包含了大部分工作中用到的常用处理方法,也有详细文档。

可以使用 Hoe 来轻松解决字符串,数字,随机数等的处理。

整个包不依赖任何其他第三方库,也极其简单小巧,只有 18kb。

JDK 版本需要 1.6 以上

github 源码地址:https://github.com/caspar-chen/hoe

文档地址:https://caspar-chen.github.io/hoe/

引用

jar 包托管在 maven 中央仓库,如果你使用了 maven,可以直接轻松引用

http://search.maven.org/#search%7Cga%7C1%7Choe

<dependency>            <groupId>com.github.caspar-chen</groupId>            <artifactId>hoe</artifactId>            <version>0.0.2</version>        </dependency>


使用

NumberHoe 数字处理工具
//最大公约数System.out.println(NumberHoe.gcd(2,8));//result = 2System.out.println(NumberHoe.gcd(12,16,40));//result = 4//最小公倍数System.out.println(NumberHoe.lcm(2,3));//result = 6System.out.println(NumberHoe.lcm(2,6,22));//result = 66//是否是负整数System.out.println(NumberHoe.isNegativeInteger("-1"));//result = true//是否是非负整数,(包括0和正整数)System.out.println(NumberHoe.isNonNegativeInteger("-1"));//result = false//是否是正整数System.out.println(NumberHoe.isPositiveInteger("22"));//result = true//是否是非正整数,(包括0和负整数)System.out.println(NumberHoe.isNonPositiveInteger("0"));//result = true//将金额转换为汉语中人民币的大写System.out.println(NumberHoe.toCNMontrayUnit(new BigDecimal(123.45)));// result = 壹佰贰拾叁元肆角伍分//将金额转换为汉语中人民币的大写System.out.println(NumberHoe.toCNMontrayUnit("-4500.23")); //result = 负肆仟伍佰元零贰角叁分

StringHoe 字符串操作
//拼接任意数量字符串System.out.println(StringHoe.append("h","o","e"));//result = "hoe"//在第一个字符串之前拼接任意数量字符串System.out.println(StringHoe.appendPre("h","o","e"));//result = "oeh"//将连续的多个空格替换成一个空格System.out.println(StringHoe.collapseWhitespace("a     b c"));//result = "a b c"//一个字符串在另一个字符串中出现的次数System.out.println(StringHoe.countMatches("bobolab", "bo"));//result = 2//将html实际符号转换成十进制的代码System.out.println(StringHoe.htmlDecodeDecimalCode("<a>♣</a>"));//result = "<a>♣</a>"//将html十进制的代码转换成 html实际符号System.out.println(StringHoe.htmlEncodeByDecimalCode("<a>♣</a>"));//result = "<a>♣</a>"//将html实际符号转换成html编码符号System.out.println(StringHoe.htmlDecodeHtmlCode("<a>♣</a>"));//result = "<a>♣</a>"//将html编码符号转换成 html实际符号System.out.println(StringHoe.htmlEncodeByHtmlCode("<a>♣</a>"));//result = "<a>♣</a>"//判断是否为null,或空字符串,或全是空格,或为大小写的字符串 "null"System.out.println(StringHoe.isEmpty(" ")); //result = true//判断是否不为空 isEmpty的想反结果System.out.println(StringHoe.isNotEmpty(" ")); //result = false//将字符串md5加密为32位的字符串System.out.println(StringHoe.md5Bit32("要加密的字符串"));//result = cbdabf4eaccbec399cb73bf63748882f//将字符串md5加密为16位的字符串System.out.println(StringHoe.md5Bit16("要加密的字符串"));//result = accbec399cb73bf6//去除字符串开头和结尾的空格System.out.println(StringHoe.trim(" a b "));//result = "a b"//去除所有的空格,制表符等System.out.println(StringHoe.trimAll(" a b c "));//result = "abc"//去除字符串开头的空格System.out.println(StringHoe.trimLeft(" a b "));//result = "a b "//去除字符串结尾的空格System.out.println(StringHoe.trimRight(" a b "));//result = " a b"//从左边开始截取字符串,截取2位System.out.println(StringHoe.left("abc", 2));//result = ab//从右边开始截取字符串,截取2位System.out.println(StringHoe.right("abc", 2));//result = bc//截取字符串,指定下标和截取位数,下标从0开始System.out.println(StringHoe.mid("abcdef", 1, 3));//result = bcd//组合数组,将数组用指定分隔符去组合成String字符串(此方法提供各种参数类型的重载)String[] arr = {"a","b","c","d"};System.out.println(StringHoe.join(arr, "-"));//result = a-b-c-d//在字符串前面加上另一个字符串,如,在561前面补0,补足5位System.out.println(StringHoe.padLeft("561", "0", 5));//result = 00561//在字符串后面加上另一个字符串,如,在561后面补0,补足5位System.out.println(StringHoe.padRight("561", "0", 5));//result = 56100//转换成驼峰命名法System.out.println(StringHoe.toCamelCase("hello world"));//result = helloWorld//转换成Studly命名法 (所有单词首字母大写)System.out.println(StringHoe.toStudlyCase("hello world"));//result = HelloWorld//反驼峰命名法 (所有单词首字母小写),用指定字符串拼接System.out.println(StringHoe.toDecamelize("hello world","-"));//result = hello-worldSystem.out.println(StringHoe.toDecamelize("hello world",null));//result = hello world//蛇行命名法 (所有单词首字母小写,下划线分割)System.out.println(StringHoe.toSnakeCase("hello world"));//result = hello_world//将字符串按行(\r\n)转换成数组StringHoe.lines("hello\r world"); //result = {"hello","world"}//反转字符串 abc -> cbaSystem.out.println(StringHoe.reverse("abc"));//result = cba//将字符串重复多少次System.out.println(StringHoe.repeat("hoe", 3));//result = hoehoehoe//将字符串重复多少次,并用指定字符串隔开System.out.println(StringHoe.repeat("hoe", "-", 3));//result = hoe-hoe-hoe//删除第一个字符System.out.println(StringHoe.removeFirst("hoe"));// result = oe//删除最后一个字符System.out.println(StringHoe.removeLast("hoe"));// result = ho//所有非数字,字母,下划线的字符System.out.println(StringHoe.removeNonWords("中国hoe*—_♣"));// result = hoe_//首字母大写System.out.println(StringHoe.upperFirst("hoe"));// result = Hoe//首字母小写System.out.println(StringHoe.lowerFirst("Hoe"));// result = hoe

RandomHoe 随机操作
//获取随机32位小写的UUIDSystem.out.println(RandomHoe.uuid());//获取随机32位大写的UUIDSystem.out.println(RandomHoe.uuidUpper());//获取随机16位小写的UUIDSystem.out.println(RandomHoe.uuidBit16());//获取随机16位大写的UUIDSystem.out.println(RandomHoe.uuidBit16Upper());//获取随机数,0到4 ,不包括5System.out.println(RandomHoe.random(5));//获取范围内的随机数,10到49,不包括50System.out.println(RandomHoe.random(10,50));//获取N位的随机数字,例子18位System.out.println(RandomHoe.randomLimit(18));//随机取数组或集合中的一个,此方法有多种参数类型重载int[] iarr = {1,2,3,4,5};System.out.println(RandomHoe.randomGet(iarr));