๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
BackEnd๐ŸŒฑ/Spring

[Spring] StringUtils ์œ ์šฉํ•œ ๋ฉ”์„œ๋“œ ์ •๋ฆฌ

by dkswnkk 2022. 8. 31.

org.springframework.util ํŒจํ‚ค์ง€์˜ StringUtils ํด๋ž˜์Šค์—๋Š” ์†์‰ฝ๊ฒŒ ๋ฌธ์ž์—ด์„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ฒŒ์‹œ๊ธ€์—์„œ๋Š” StringUtils์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฌธ์ž์—ด์„ ํ˜•์‹์„ ์ฒดํฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ช‡ ๊ฐ€์ง€ ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
StringUtils์˜ ๋ชจ๋“  ๋ฉ”์„œ๋“œ๋Š” ์•„๋ž˜์˜ ๊ณต์‹ ๋ฌธ์„œ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

StringUtils (Spring Framework 5.3.22 API)

Check whether the given CharSequence contains actual text. More specifically, this method returns true if the CharSequence is not null, its length is greater than 0, and it contains at least one non-whitespace character. StringUtils.hasText(null) = false S

docs.spring.io

 

1. isEmpty()

๋ฌธ์ž์—ด์ด null์ธ์ง€ ""์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

@Test
@DisplayName("isEmpty")
public void isEmptyTest() {
    assertTrue(StringUtils.isEmpty(null));
    assertTrue(StringUtils.isEmpty(""));
    assertFalse(StringUtils.isEmpty(" ")); // ๊ณต๋ฐฑ์€ False
}

 

2. hasLength()

๋ฌธ์ž์—ด์ด null์ธ์ง€ ""์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
isEmpty()๋Š” " "์™€ ๊ฐ™์€ ๊ณต๋ฐฑ ๋ฌธ์ž์—ด์— ๊ด€ํ•ด False๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐ˜๋ฉด hasLength()๋Š” True๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
isEmpty() ๊ฐ™์€ ๊ฒฝ์šฐ ํ˜„์žฌ Deprecated ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๊ณต์‹๋ฌธ์„œ์—์„œ๋Š” hasLength()๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, hasText()๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๊ถŒ์žฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

@Test
@DisplayName("hasLength")
public void hasLengthTest() {
    assertTrue(StringUtils.hasLength("๊ธธ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ"));
    assertTrue(StringUtils.hasLength(" ")); // ๊ณต๋ฐฑ์€ True
    assertFalse(StringUtils.hasLength(""));
    assertFalse(StringUtils.hasLength(null));
}

 

3. hasText()

๋ฌธ์ž์—ด์ด ์ง„์ •ํ•œ Textํ˜•ํƒœ์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, null์„ ํฌํ•จํ•ด์„œ ๊ณต๋ฐฑ๋งŒ ์กด์žฌํ•œ๋‹ค๋ฉด False๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
hasText()๊ฐ€ ๊ต‰์žฅํžˆ ์œ ์šฉํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋Š”๋ฐ, ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ธ์ง€ if๋ฌธ์„ ๋ฒ”๋ฒ…์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@Test
@DisplayName("hasText")
public void hasTestTest() {
    assertTrue(StringUtils.hasText("๋ฌธ์ž์—ด"));
    assertFalse(StringUtils.hasText(""));
    assertFalse(StringUtils.hasText(" "));  // ๊ณต๋ฐฑ์€ False
    assertFalse(StringUtils.hasText(null));
}

 

4. containsWhitespace()

๋ฌธ์ž์—ด์ด ๊ณต๋ฐฑ์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ์•„๋‹Œ์ง€๋ฅผ ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค.

@Test
@DisplayName("containsWhitespace")
public void containsWhitespaceTest() {
    /*
        hasLength()๋ฅผ ๋จผ์ € ํ˜ธ์ถœ ํ›„, True์ผ์‹œ ๊ณต๋ฐฑ์„ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค.
     */
    assertTrue(StringUtils.containsWhitespace("๊ณต๋ฐฑ์€ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค."));
    assertFalse(StringUtils.containsWhitespace("๊ณต๋ฐฑ์€ํฌํ•จํ•˜์ง€์•Š๊ณ ์žˆ์Šต๋‹ˆ๋‹ค."));
    assertFalse(StringUtils.containsWhitespace(""));
    assertFalse(StringUtils.containsWhitespace(null));
}

 

5. trimLeadingWhitespace()

๋ฌธ์ž์—ด ์•ž์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

@Test
@DisplayName("trimLeadingWhitespace")
public void trimLeadingWhitespaceTest() {
    assertThat("๋ฌธ์ž์—ด ์•ž์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.").isEqualTo(StringUtils.trimLeadingWhitespace("   ๋ฌธ์ž์—ด ์•ž์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค."));
}

 

6. trimTrailingWhitespace()

๋ฌธ์ž์—ด ๋’ค์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

@Test
@DisplayName("trimTrailingWhitespace")
public void trimTrailingWhitespaceTest() {
    assertThat("๋ฌธ์ž์—ด ๋’ค์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.").isEqualTo(StringUtils.trimTrailingWhitespace("๋ฌธ์ž์—ด ๋’ค์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.   "));
}

 

7. trimWhitespace()

๋ฌธ์ž์—ด ์•ž ๋’ค์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

@Test
@DisplayName("trimWhitespace")
public void trimWhitespaceTest() {
    assertThat("๋ฌธ์ž์—ด ์•ž๋’ค๋กœ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.").isEqualTo(StringUtils.trimWhitespace("   ๋ฌธ์ž์—ด ์•ž๋’ค๋กœ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.  "));
}

 

8. trimAllWhitespace()

๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

@Test
@DisplayName("trimAllWhitespace")
public void trimAllWhitespace() {
    assertThat("๋ฌธ์ž์—ด์˜๋ชจ๋“ ๊ณต๋ฐฑ์„์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.").isEqualTo(StringUtils.trimAllWhitespace("   ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค."));
}

 

9. trimLeadingCharacter()

๋ฌธ์ž์—ด์—์„œ ์„ ํ–‰ ๊ธ€์ž๋ฅผ ํ•˜๋‚˜ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

 @Test
 @DisplayName("trimLeadingCharacter")
 public void trimLeadingCharacterTest() {
     assertThat("๋ณ„ํ‘œ์ œ๊ฑฐ").isEqualTo(StringUtils.trimLeadingCharacter("*๋ณ„ํ‘œ์ œ๊ฑฐ", '*'));
 }

 

10. trimTrailingCharacter()

๋ฌธ์ž์—ด์˜ ํ›„ํ–‰ ๊ธ€์ž๋ฅผ ํ•˜๋‚˜ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

@Test
@DisplayName("trimTrailingCharacter")
public void trimTrailingCharacterTest() {
    assertThat("๋ณ„ํ‘œ์ œ๊ฑฐ").isEqualTo(StringUtils.trimTrailingCharacter("๋ณ„ํ‘œ์ œ๊ฑฐ*", '*'));
}

 

11. countOccurrencesOf()

๋ฌธ์ž์—ด์—์„œ ์ง€์ •ํ•œ ๊ธ€์ž๊ฐ€ ๋ช‡ ๊ฐœ๊ฐ€ ๋‚˜์˜ค๋Š”์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.

@Test
@DisplayName("countOccurrencesOf")
public void countOccurrencesOfTest() {
    assertThat(StringUtils.countOccurrencesOf("apple", "p")).isEqualTo(2);
    assertThat(StringUtils.countOccurrencesOf("apple", "c")).isEqualTo(0);
}

 

12. startWithIgnoreCase()

๋ฌธ์ž์—ด์ด ์ง€์ •ํ•œ ๋ฌธ์ž์—ด๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.

@Test
@DisplayName("startWithIgnoreCase")
public void startWithIgnoreCaseTest() {
    assertTrue(StringUtils.startsWithIgnoreCase("010-1234-5678", "010"));
    assertFalse(StringUtils.startsWithIgnoreCase("011-1234-5678", "010"));
}

 

13. endsWithIgnoreCase()

๋ฌธ์ž์—ด์ด ์ง€์ •ํ•œ ๋ฌธ์ž์—ด๋กœ ๋๋‚˜๋Š”์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.

@Test
@DisplayName("endsWithIgnoreCase")
public void endsWithIgnoreCaseTest() {
    assertTrue(StringUtils.endsWithIgnoreCase("010-1234-5678", "5678"));
    assertFalse(StringUtils.endsWithIgnoreCase("010-1234-5678", "1234"));
}

 

์ „์ฒด ์ฝ”๋“œ

package com.example.javapractice;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.StringUtils;

import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class JavapracticeApplicationTests {

    @Test
    @DisplayName("isEmpty")
    public void isEmptyTest() {
        assertTrue(StringUtils.isEmpty(null));
        assertTrue(StringUtils.isEmpty(""));
        assertFalse(StringUtils.isEmpty(" ")); // ๊ณต๋ฐฑ์€ False
    }

    @Test
    @DisplayName("hasLength")
    public void hasLengthTest() {
        assertTrue(StringUtils.hasLength("๊ธธ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ"));
        assertTrue(StringUtils.hasLength(" ")); // ๊ณต๋ฐฑ์€ True
        assertFalse(StringUtils.hasLength(""));
        assertFalse(StringUtils.hasLength(null));
    }

    @Test
    @DisplayName("hasText")
    public void hasTestTest() {
        assertTrue(StringUtils.hasText("๋ฌธ์ž์—ด"));
        assertFalse(StringUtils.hasText(""));
        assertFalse(StringUtils.hasText(" "));  // ๊ณต๋ฐฑ์€ False
        assertFalse(StringUtils.hasText(null));
    }

    @Test
    @DisplayName("containsWhitespace")
    public void containsWhitespaceTest() {
        /*
            hasLength()๋ฅผ ๋จผ์ € ํ˜ธ์ถœ ํ›„, True์ผ์‹œ ๊ณต๋ฐฑ์„ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค.
         */
        assertTrue(StringUtils.containsWhitespace("๊ณต๋ฐฑ์€ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค."));
        assertFalse(StringUtils.containsWhitespace("๊ณต๋ฐฑ์€ํฌํ•จํ•˜์ง€์•Š๊ณ ์žˆ์Šต๋‹ˆ๋‹ค."));
        assertFalse(StringUtils.containsWhitespace(""));
        assertFalse(StringUtils.containsWhitespace(null));
    }

    @Test
    @DisplayName("trimLeadingWhitespace")
    public void trimLeadingWhitespaceTest() {
        assertThat("๋ฌธ์ž์—ด ์•ž์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.").isEqualTo(StringUtils.trimLeadingWhitespace("   ๋ฌธ์ž์—ด ์•ž์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค."));
    }

    @Test
    @DisplayName("trimTrailingWhitespace")
    public void trimTrailingWhitespaceTest() {
        assertThat("๋ฌธ์ž์—ด ๋’ค์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.").isEqualTo(StringUtils.trimTrailingWhitespace("๋ฌธ์ž์—ด ๋’ค์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.   "));
    }

    @Test
    @DisplayName("trimWhitespace")
    public void trimWhitespaceTest() {
        assertThat("๋ฌธ์ž์—ด ์•ž๋’ค๋กœ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.").isEqualTo(StringUtils.trimWhitespace("   ๋ฌธ์ž์—ด ์•ž๋’ค๋กœ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.  "));
    }

    @Test
    @DisplayName("trimAllWhitespace")
    public void trimAllWhitespace() {
        assertThat("๋ฌธ์ž์—ด์˜๋ชจ๋“ ๊ณต๋ฐฑ์„์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.").isEqualTo(StringUtils.trimAllWhitespace("   ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค."));
    }

    @Test
    @DisplayName("trimLeadingCharacter")
    public void trimLeadingCharacterTest() {
        assertThat("๋ณ„ํ‘œ์ œ๊ฑฐ").isEqualTo(StringUtils.trimLeadingCharacter("*๋ณ„ํ‘œ์ œ๊ฑฐ", '*'));
    }

    @Test
    @DisplayName("trimTrailingCharacter")
    public void trimTrailingCharacterTest() {
        assertThat("๋ณ„ํ‘œ์ œ๊ฑฐ").isEqualTo(StringUtils.trimTrailingCharacter("๋ณ„ํ‘œ์ œ๊ฑฐ*", '*'));
    }

    @Test
    @DisplayName("countOccurrencesOf")
    public void countOccurrencesOfTest() {
        assertThat(StringUtils.countOccurrencesOf("apple", "p")).isEqualTo(2);
        assertThat(StringUtils.countOccurrencesOf("apple", "c")).isEqualTo(0);
    }

    @Test
    @DisplayName("startWithIgnoreCase")
    public void startWithIgnoreCaseTest() {
        assertTrue(StringUtils.startsWithIgnoreCase("010-1234-5678", "010"));
        assertFalse(StringUtils.startsWithIgnoreCase("011-1234-5678", "010"));
    }

    @Test
    @DisplayName("endsWithIgnoreCase")
    public void endsWithIgnoreCaseTest() {
        assertTrue(StringUtils.endsWithIgnoreCase("010-1234-5678", "5678"));
        assertFalse(StringUtils.endsWithIgnoreCase("010-1234-5678", "1234"));
    }

}

๋Œ“๊ธ€