Find the length of a substring of a string

For example, this question:

There is the following string, in which several substrings are separated by "|". Split each substring, and then find the substring with length 4.

For example: Suppose the entire string is aaa|bbbb|cccc|dd|eeee, then there are three substrings with a length of 4, respectively

bbbb

cccc

eeee

code show as below:

public static void main(String[] args) throws IOException {

		String str="aaa|bbbb|cccc|dd|eeee";
		String[] arr=str.split("\\|");
		for(int i=0;i<arr.length;i++)
		{
			String temp=arr[i];
			if(temp.length()==4)
			{
				System.out.println(temp);
			}
		}
		
		
	}


It can also be written like this:

public static void main(String[] args) {
        String str = "aaa|bbbb|cccc|dd|eeee";
        String[] arr = str.arr("\\|");
        for (int i = 0; i < arr.length; i++) {
            if(arr[i].length()==4){
                System.out.println(arr[i]);
            }
        }
    }

Guess you like

Origin blog.csdn.net/m0_63715487/article/details/127938698