'remove empty'에 해당되는 글 1건

  1. 2022.08.10 String split 사용할때 빈값 삭제 / 유지 예제 코드

String split 사용할때 빈값 삭제 / 유지 예제 코드

프로그래밍/java 2022. 8. 10. 22:26
반응형

String split 사용할때 빈값 삭제 / 유지 예제 코드

        String test1 = "abc|aa|bb|cc||";

        String[] strArray = test1.split("\\|");
        System.out.println("1. test1 length :" + strArray.length);

        for (String str : strArray) {
            System.out.println("1. test1 : " + str);
        }

        System.out.println("");

        strArray = test1.split("\\|", -1);
        System.out.println("2. test1 length :" + strArray.length);

        for (String str : strArray) {
            System.out.println("2. test1 : " + str);
        }

 

실행 결과

1. test1 length :4
1. test1 : abc
1. test1 : aa
1. test1 : bb
1. test1 : cc

2. test1 length :6
2. test1 : abc
2. test1 : aa
2. test1 : bb
2. test1 : cc
2. test1 : 
2. test1 :

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-int-

 

String (Java Platform SE 8 )

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argum

docs.oracle.com

 

반응형
: