Hutool使用指南(二):字符串工具

阅读本篇文章大约花费您5分钟!


     今天是Hutool工具的第二篇文章,介绍一下字符串相关的处理方法。字符串工具指cn.hutool.core.util.StrUtil类,其中对String的多种方法进行了封装并且提供了其他方便实用的方法。StrUtil中的方法是静态方法。

从多个字符串中判断是否有空

      这里的空有两层含义:一是null或者“”(空串),二是不可见字符构成的字符串(不可见字符串),比如由空格构成的字符串(“      ”)。针对两种情况Hutool提供了不同的方法来解决。常用的方法如下表所示:

多个字符串判断是否有空

isBlank(CharSequence arg0):boolean

判断字符串是否为null或者””或者是不可见字符串

isEmpty(charSequence arg0):boolean

判断字符串是否为null或者””

hasBlank(CharSequence…arg0):boolean

判断多个字符串中是否有null或者””或者是不可见字符串

hasEmpty(CharSequence…arg0):boolean

判断多个字符串中是否有null或者””

     其中在表单登录时,hasEmpty()方法就可以一展身手了。

     下面看一下示例代码:

public class Test{
    public static void testStrUtil01() {
	StrUtil strUtil=new StrUtil();
	System.out.println("hasEmpty判断多个字符串中是否有null或者\"\":");
	System.out.println(strUtil.hasEmpty("smart","pig",""));
	System.out.println("hasBlank判断多个字符串是否有null或者\"\"或者不可见字符:");
	System.out.println(strUtil.hasBlank("smart","pig","  "));
	System.out.println(strUtil.isEmpty(" "));
	System.out.println(strUtil.isBlank(" "));
    }
    
    public static void main(String[] args) {
	Test.testStrUtil01();
        /*结果如下:
        hasEmpty判断多个字符串中是否有null或者"":
        true
        hasBlank判断多个字符串是否有null或者""或者不可见字符:
        true
        false
        true
        */
    }

}    

sub方法

      sub()方法相当于subString()方法;

      有三个参数:第一个是截取的字符串,后两个是首尾位置。sub()方法有一个最大的特点就是容错率非常高,并且-1表示最后一个字符,-2表示倒数第二个字符,以此类推。   并且首尾位置也可以颠倒。

     看一个例子就可以完全理解:

public class Test{
    public static void testStrUtil02() {
	String testStr="my name is smartPig";
	String result01=StrUtil.sub(testStr, 0, 4);
	/*虽然是4-0,但是实际上还是算成0-4*/
	String result02=StrUtil.sub(testStr, 4, 0);
	String result03=StrUtil.sub(testStr, -1, 3);
	String result04=StrUtil.sub(testStr, -4, 3);
		
	System.out.println(result01);
	System.out.println(result02);
	System.out.println(result03);
	System.out.println(result04);
    }

    public static void main(String[] args) {
	Test.testStrUtil02();
        /*结果为:
        my n
        my n
        name is smartPi
        name is smar
        */
    }
}

format方法

      format方法类似于JDBC中PreparedStatement中的?占位符,可以在字符串中使用”{}”作为占位符

      看个例子就明白了!   

public class Test{
    public static void testStrUtil04() {
	String testStr="{} name is smart{}";
	String result=StrUtil.format(testStr, "my","Pig");
        //my对应第一个{},Pig对应第二个{}
	System.out.println(result);
    }
    public static void main(String[] args) {
	Test.testStrUtil04();
        /*结果如下:
	my name is smartPig
	*/
    }
}

有了这个方法,就不需要在某些场景辛苦的和参数拼接字符串了。

字符串逆序

      Hutool可以很容易的实现字符串逆序。可以使用StrUtil.reverse(String str):String方法。

去除前后缀

      Hutool可以去除字符串的前缀/后缀,这个特点非常适用于去除文件的后缀名。相关方法如下表:

去除前后缀

removeSuffix(CharSequence str, CharSequence suffix)

去除后缀

removeSuffixIgnoreCase(CharSequence str, CharSequence suffix)

去除后缀,忽略大小写

removePrefix(CharSequence str, CharSequence suffix)

去除前缀

removePrefixIgnoreCase(CharSequence str, CharSequence suffix)

去除前缀,忽略大小写

public class Test{
    public static void testStrUtil06() {
	String path1="smart.png";
	String path2="smart.PNg";
        System.out.println(StrUtil.removeSuffix(path1, ".png"));
        System.out.println(StrUtil.removeSuffixIgnoreCase(path2,".png"));
    }

    public static void main(String[] args) {
	Test.testStrUtil06();
        /*
	去除前后缀结果:
        smart
        smart
        */
    }
}

字符串常量

     Hutool定义了一些字符常量,可以灵活使用。部分常量如下所示:

一些常量

StrUtil.DOT

StrUtil.DOUBLE_DOT

双点

StrUtil.UNDERLINE

下划线

StrUtil.EMPTY

横杠

StrUtil.BACKSLASH

反斜杠

StrUtil.DASHED

破折

StrUtil.BRACKET_END

右中扩号

StrUtil.BRACKET_START

左中括号

StrUtil.COLON

冒号

StrUtil.COMMA

逗号

StrUtil.DELIM_END

右大括号

StrUtil.DELIM_START

左大括号

public class Test{
    public static void testStrUtil07() {
	System.out.println(StrUtil.DOT);
	System.out.println(StrUtil.DOUBLE_DOT);
	System.out.println(StrUtil.UNDERLINE);
	System.out.println(StrUtil.EMPTY);
	System.out.println(StrUtil.BACKSLASH);
	System.out.println(StrUtil.DASHED);
	System.out.println(StrUtil.BRACKET_END);
	System.out.println(StrUtil.BRACKET_START);
	System.out.println(StrUtil.COLON);
	System.out.println(StrUtil.COMMA);
	System.out.println(StrUtil.DELIM_END);
	System.out.println(StrUtil.DELIM_START);
    }
	
    public static void main(String[] args) {
	Test.testStrUtil07();
	/*结果如下:
	.
	..
	_
	
	\
	-
	]
	[
	:
	,
	}
	{	
	*/
    }
}

      今天的字符串相关方法就介绍到这里,下一次会给大家介绍随机工具类的使用方法!

      今天在《码农翻身》一书中看到了这样一句话:凡事必将骑上虎背。不逼自己一次,怎么知道自己真正的实力!也把这句话送给正在努力的各位!希望各位能够不离不弃,勇于坚持,勤于实践!

 

 

猜你喜欢

转载自blog.csdn.net/tianc_pig/article/details/87944463