Skip to content

02. StrUtil

晓寻遥 edited this page Nov 6, 2021 · 1 revision

Introduce

This is a more convenient operation for String. Allows us to complete our needs without having to design some of the functions.

These methods are

  • hasBlank
  • removePrefix
  • subString
  • format
  • eval
  • ......

next, let's see how to use each method.

Method

1. hasBlank and hasEmpty

Introduce

It is to give some strings and return true if there are empty ones. It is often used to judge whether many fields are empty (such as web form data).

The difference between these two methods is that hasEmpty only judges whether it is a null or an empty string (""), and hasBlank will also count invisible characters as empty. The same is true for isEmpty and isBlank.

Example

public void empty_and_blank(){
		String a = "";
		String b = " ";
		String c = "hello world";
		String d = "hello";

		// is `a` hasEmpty --> true
		System.out.println( "is `a` hasEmpty --> " + StrUtil.hasEmpty(a));
		// is `b` hasEmpty --> false
		System.out.println( "is `b` hasEmpty --> " + StrUtil.hasEmpty(b));
		// is `c` hasEmpty --> false
		System.out.println( "is `c` hasEmpty --> " + StrUtil.hasEmpty(c));
		// is `d` hasEmpty --> false
		System.out.println( "is `d` hasEmpty --> " + StrUtil.hasEmpty(d));

		// is `a` hasBlank --> true
		System.out.println( "is `a` hasBlank --> " + StrUtil.hasBlank(a));
		// is `b` hasBlank --> true
		System.out.println( "is `b` hasBlank --> " + StrUtil.hasBlank(b));
		// is `c` hasBlank --> false
		System.out.println( "is `c` hasBlank --> " + StrUtil.hasBlank(c));
		// is `d` hasBlank --> false
		System.out.println( "is `d` hasBlank --> " + StrUtil.hasBlank(d));
}

2. removePrefix and removeSuffix

Introduce

This is how to remove String prefix and suffix.

can be used to remove the suffix of the file name.

Example

public void removePrefix_and_removeSuffix(){
		String fileName = "portrait.jpg";
		// is `fileName` removeSuffix --> portrait
		System.out.println(StrUtil.removeSuffix( "is `fileName` removeSuffix --> " + fileName, ".jpg"));
}

3. subString

Introduce

of course, some students may say that there is already a subString, why is there a similar method in xiaoTools, because subString will display too many abnormalities and need to judge by yourself, so we made a subString method to intercept characters string

Example

public void subString(){
		String a = "abcdefg";

		// `a` subString 2 and 3 --> c
		System.out.println( "`a` subString 2 and 3 --> " + StrUtil.subString(a, 2, 3));
		// `a` subString -1 and 1 --> bcdef
		System.out.println( "`a` subString -1 and 1 --> " + StrUtil.subString(a, -1, 1));
		// `a` subString 2 and -3 --> cd
		System.out.println( "`a` subString 2 and -3 --> " + StrUtil.subString(a, 2, -3));
}

4. format

Introduce

this is a method based on a lot of inspiration. I believe all students will find the beauty of this method.

Example

public void format(){
  		//this is format method
		System.out.println(StrUtil.format("{} is {} method","this","format"));
}

5. eval

Introduce

I don’t know if you are very envious of the eval method in python that can directly calculate the string in the String mode. Now java is also available, as simple as python.

Example

public void test_eval(){
		String a = "(1 + 2) * 4";
                // (1 + 2) * 4 = 12
		System.out.println( "(1 + 2) * 4 = " + StrUtil.eval(a));
}

6. more

to learn more, refer to the API documentation