๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐‘†๐‘ข๐‘›๐‘ โ„Ž๐‘–๐‘›๐‘’ ๐‘Ž๐‘“๐‘ก๐‘’๐‘Ÿ ๐‘Ÿ๐‘Ž๐‘–๐‘›โœง

[Java] ๋ฌธ์ž์—ด ๋ณธ๋ฌธ

โญ๋ฌธ์ž์—ด

  • ์ดˆ๊ธฐํ™”
    • String s = “hello”
      • ์ƒ์ˆ˜ํ’€์— “hello”๋ผ๋Š” ๋ฌธ์ž์—ด์ด ์žˆ๋Š”์ง€ ํ™•์ธ, ์—†์œผ๋ฉด ์ƒ์ˆ˜ํ’€์— ์ƒˆ๋กœ ๋งŒ๋“ฆ
    • String s = new String("hello");
      • ํž™ ์˜์—ญ์— “hello”๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑ
public class StringInitExample {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "hello";
        String s3 = new String("hello");
        String s4 = new String("hello");

        // ์ฃผ์†Œ๊ฐ’ ๋น„๊ต
        System.out.println(s1 == s2); // true
        System.out.println(s3 == s4); // false

        // ๋‚ด์šฉ๋ฌผ ๋น„๊ต
        System.out.println(s1.equals(s2)); // true
        System.out.println(s3.equals(s4)); // true
    }
}

๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ์˜์—ญ

  • ์ŠคํŠธ๋ง ํ’€
    • ์ŠคํŠธ๋ง ๋ฆฌํ„ฐ๋Ÿด์€ ์žฌ์‚ฌ์šฉ๋  ๊ฐ€๋Šฅ์„ฑ์ด ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ์ŠคํŠธ๋ง ํ’€์— ์ €์žฅ
    • ๋งŒ์•ฝ ์žฌ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ŠคํŠธ๋ง ํ’€์— ์ €์žฅ๋œ ์ŠคํŠธ๋ง ๋ฆฌํ„ฐ๋Ÿด ์žฌ์‚ฌ์šฉ String interning
  • ๋ฉ”๋ชจ๋ฆฌ ์ €์žฅ
    • ๋ฌธ์ž์—ด ์ €์žฅ์„ ์œ„ํ•ด ๊ฐ ๋ฌธ์ž์— ํ•ด๋‹นํ•˜๋Š” byte ๋ฐฐ์—ด๋กœ ์ €์žฅ
      • String s = "hello";

        // s - ๊ฐ์ฒด ๋ณ€์ˆ˜
        // "hello" - ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด
    • ๊ฐ์ฒด ๋ณ€์ˆ˜๋Š” ์Šคํƒ ๊ณต๊ฐ„์— ์ €์žฅ
    • ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ String Pool์— ์ €์žฅ
    • ๋ฌธ์ž์—ด ์ƒ์ˆ˜์  ํŠน์ง•
      • ๋ณดํ†ต ๋ฌธ์ž์—ด์€ ์™„์„ฑ๋œ ๋ฌธ์ž์—ด ์ƒ์ˆ˜๋ฅผ ์ €์žฅ → String Pool์ €์žฅ
public class StringConcatExample {
    public static void main(String[] args) {
        String s = "hello";
        // s.charAt(0) = 'H'; // ์—๋Ÿฌ. ์ผ๋ถ€ ์›์†Œ ์ˆ˜์ • ๋ถˆ๊ฐ€๋Šฅ

        s += "world"; // ๋‹ค์‹œ ์žฌํ• ๋‹นํ•ด์•ผ ํ•จ
        // ๋‹ค์‹œ ์žฌํ• ๋‹นํ•˜๋ฉด ์ƒ์ˆ˜ํ’€์— "Hello"๋ผ๋Š” ๋ฌธ์ž์—ด ๊ฐ์ฒด ์ƒ์„ฑํ•˜๊ณ  ์ƒ์„ฑํ•œ ๊ฐ์ฒด์˜ ์ฃผ์†Œ๊ฐ’ ์ €์žฅ
    }
}

 

  • ๋ฌธ์ž์—ด ๊ด€๋ จ ํ•จ์ˆ˜ 
    • equals() - ๋ฌธ์ž์—ด ๋‚ด์šฉ๋ฌผ ๋™์ผ ์—ฌ๋ถ€ ํ™•์ธ(๋ฌธ์ž ๋น„๊ตํ• ๋•Œ๋Š” ''(์ž‘์€๋”ฐ์Œํ‘œ) ์‚ฌ์šฉํ•ด์•ผํ•จ)
    • length() - ๋ฌธ์ž์—ด ๊ธธ์ด ๋ฐ˜ํ™˜
    • charAt() - ํŠน์ • ์ธ๋ฑ์Šค์˜ ๋ฌธ์ž ๋ฐ˜ํ™˜
    • indexOf() - ํŠน์ • ๋ฌธ์ž์˜ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
    • contains() - ๋ฌธ์ž์—ด ํฌํ•จ ์—ฌ๋ถ€ ํ™•์ธ
    • compareTo() - ๋ฌธ์ž์—ด ์‚ฌ์ „์‹ ๋น„๊ต (์ž‘์œผ๋ฉด ์Œ์ˆ˜, ๊ฐ™์œผ๋ฉด 0, ๋’ค์— ์žˆ์œผ๋ฉด ์–‘์ˆ˜)
    • matches() - ์ •๊ทœ ํ‘œํ˜„์‹ ์ผ์น˜ ์—ฌ๋ถ€ ํ™•์ธ ์ •๊ทœ ํ‘œํ˜„์‹ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    • replace(๊ธฐ์กด ๋ฌธ์ž์—ด, ๋ณ€๊ฒฝํ•  ๋ฌธ์ž์—ด), replaceAll(), replaceFirst() - ๋ฌธ์ž์—ด ์น˜ํ™˜ ์ •๊ทœ ํ‘œํ˜„์‹ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    • substring(์‹œ์ž‘์ธ๋ฑ์Šค., ๋ ์ธ๋ฑ์Šค) - ์ผ๋ถ€ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜
    • split() - ์ •๊ทœ ํ‘œํ˜„์‹ ๋˜๋Š” ํŠน์ • ๋ฌธ์ž ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด ๋‚˜๋ˆ” ์ •๊ทœ ํ‘œํ˜„์‹ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    • trim(), strip() - ์–‘๋ณ€์— ์žˆ๋Š” ๊ณต๋ฐฑ ์ œ๊ฑฐ ํ›„ ๋ฐ˜ํ™˜ ์›น ๊ฐœ๋ฐœ
    • toCharArray() - ๋ฌธ์ž์—ด → ๋ฌธ์ž ๋ฐฐ์—ด
    • toLowerCase(), toUpperCase() - ๋ฌธ์ž์—ด ์†Œ๋ฌธ์ž / ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜
      • // ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ์—†์ด ๋ฌธ์ž์—ด ๋™๋“ฑ๋น„๊ต
      • System.out.println(s1.toLowerCase().equals(s2.toLowerCase())); System.out.println(s1.equalsIgnoreCase(s2)); // ์œ„ ์ฝ”๋“œ์™€ ๋™์ผ
    • isEmpty(), isBlank() - ๋ฌธ์ž์—ด ๊ธธ์ด 0์ธ์ง€ ํ™•์ธ / ๋ฌธ์ž์—ด ๋น„์–ด์žˆ๊ฑฐ๋‚˜ whitespace์ธ์ง€ ํ™•์ธ
public class StringIsEmptyExample {
    public static void main(String[] args) {
        // isEmpty: ๋ฌธ์ž์—ด ๊ธธ์ด ํ™•์ธ
        // isBlank: ๋ฌธ์ž์—ด์ด ๋น„์–ด์žˆ๊ฑฐ๋‚˜ whitespace ๋ฌธ์ž์ธ์ง€ ํ™•์ธ

        String s1 = "";
        String s2 = "\t\n   ";
        System.out.println(s1.isEmpty()); // true
        System.out.println(s2.isEmpty()); // ๋ฌธ์ž์—ด ๊ธธ์ด 0์ด ์•„๋‹˜. false
        System.out.println(s1.isBlank()); // true
        System.out.println(s2.isBlank()); // white space ์กด์žฌ. true
    }
}

 

  • ๋ฌธ์ž์—ด์„ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐํƒ€์ž…์œผ๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•
    • String → byte - Byte.parseByte(String str)
    • String → short - Short.parseShort(String str)
    • String → int - Integer.parseInt(String str)
    • Strign → long → Long.parseLong(String str)
    • String → float - Float.parseFloat(String str)
    • String → double - Double.parseDouble(String str)
public class StringIsEmptyExample {
    public static void main(String[] args) {
        // isEmpty: ๋ฌธ์ž์—ด ๊ธธ์ด ํ™•์ธ
        // isBlank: ๋ฌธ์ž์—ด์ด ๋น„์–ด์žˆ๊ฑฐ๋‚˜ whitespace ๋ฌธ์ž์ธ์ง€ ํ™•์ธ

        String s1 = "";
        String s2 = "\t\n   ";
        System.out.println(s1.isEmpty()); // true
        System.out.println(s2.isEmpty()); // ๋ฌธ์ž์—ด ๊ธธ์ด 0์ด ์•„๋‹˜. false
        System.out.println(s1.isBlank()); // true
        System.out.println(s2.isBlank()); // white space ์กด์žฌ. true
    }
}
import java.util.Arrays;

public class StringToCharArrayExample {
    public static void main(String[] args) {
        // String ์ˆ˜์ • ๋ถˆ๊ฐ€ (String Pool์— ์ €์žฅ)
        String str = "this is constant string. Immutable";
        // str.charAt(0) = 'T'; // ๋ถˆ๊ฐ€๋Šฅ

        char[] charArr = str.toCharArray();
        System.out.println(Arrays.toString(charArr));

        // char[]์€ ์ˆ˜์ • ๊ฐ€๋Šฅ (ํž™์— ์ €์žฅ)
        // StringBuilder, StringBuffer๋„ char[]๋กœ ๊ตฌํ˜„๋˜์–ด์žˆ์Œ
        charArr[0] = 'T'; // ๊ฐ€๋Šฅ
        System.out.println(Arrays.toString(charArr));
        String res = String.valueOf(charArr); // ์ˆ˜์ •์™„๋ฃŒ. ๋‹ค์‹œ ์ƒ์ˆ˜ํ™”ํ•˜๊ธฐ
        System.out.println(res);
    }
}

 

  • StringBuffer & StringBuilder
    • String ๊ฐ์ฒด๋Š” ํ•œ๋ฒˆ ์ƒ์„ฑ๋˜๋ฉด ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์ด ๋ณ€ํ•˜์ง€ ์•Š์Œ
    • ๋ฌธ์ž์—ด ๋ณ€๊ฒฝ, ์ˆ˜์ •์ด ๋งŽ์€๊ฒฝ์šฐ StringBuffer, StringBuilder ์‚ฌ์šฉ 
    • ๋ฐ์ดํ„ฐ๋ฅผ Heap์— ์ €์žฅํ•œ๋‹ค
    • StringBuffer ์™€ StringBuilder ์ฐจ์ด
      • StringBuffer๋Š” ๋™๊ธฐํ™” ํ‚ค์›Œ๋“œ๋ฅผ ์ง€์›ํ•˜์—ฌ ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ์•ˆ์ „ํ•˜๋‹ค
      • StringBuilder๋Š” ๋™๊ธฐํ™”๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์•„ ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ์ ํ•ฉํ•˜์ง€ ์•Š๋‹ค. ํ•˜์ง€๋งŒ ๋™๊ธฐํ™”๋ฅผ ๊ณ ๋ คํ•˜์ง€ ๋‹จ์ผ์“ฐ๋ ˆ๋“œ์—์„œ์˜ ์„ฑ๋Šฅ์€ StringBuffer ๋ณด๋‹ค ๋›ฐ์–ด๋‚˜๋‹ค.

 

โญ์ •๊ทœํ‘œํ˜„์‹

 

  • ์ •๊ทœ ํ‘œํ˜„์‹์€ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ํฌ๋งท์„ ํ™•์ธํ•˜๊ธฐ ์ข‹์Œ
  • if๋ฌธ๊ณผ ๋น„์Šทํ•˜๊ฒŒ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์˜ค์ž๋งˆ์ž ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
  • ์ฝ”ํ…Œ์—์„œ๋„ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
  • ์ •๊ทœ ํ‘œํ˜„์‹

์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ •๊ทœ ํ‘œํ˜„์‹

  • Pattern
    • ์ •๊ทœ์‹์„ ํŒจํ„ด ํด๋ž˜์Šค๋กœ ์ปดํŒŒ์ผ
  • Matcher
    • ํŒจํ„ด์„ ํ•ด์„ํ•˜์—ฌ ๋ฌธ์ž์—ด์— ๋งž์ถฐ๋ณด๋Š” matching ์ž‘์—…
    • ์ฃผ์š” ๋ฉ”์„œ๋“œ
     

  • ์˜ˆ์ œ
import java.util.regex.Pattern;

public class PatternClassExample {
    static final String EMAIL_REGEX = "^\\w+@\\w+\\.\\w+(\\.\\w+)?$";
    static final String PHONE_REGEX = "^\\d{2,3}-\\d{3,4}-\\d{4}$";
    public static void main(String[] args) {
        String email = "qwerty@gmail.com";
        System.out.println("Pattern.matches(EMAIL_REGEX, email) = " + Pattern.matches(EMAIL_REGEX, email));

        email = "qweqw@naver.co.kr";
        System.out.println("Pattern.matches(EMAIL_REGEX, email) = " + Pattern.matches(EMAIL_REGEX, email));

        email = "qwe@naver.";
        System.out.println("Pattern.matches(EMAIL_REGEX, email) = " + Pattern.matches(EMAIL_REGEX, email));
        System.out.println();


        String phone = "010-1234-5678";
        System.out.println("Pattern.matches(PHONE_REGEX, phone) = " + Pattern.matches(PHONE_REGEX, phone));

        phone = "02-000-0000";
        System.out.println("Pattern.matches(PHONE_REGEX, phone) = " + Pattern.matches(PHONE_REGEX, phone));

        phone = "010-00000-0000";
        System.out.println("Pattern.matches(PHONE_REGEX, phone) = " + Pattern.matches(PHONE_REGEX, phone));
    }
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MatcherClassExample1 {
    static final String ALPHA_REGEX = "^[a-zA-Z]*$";
    static final String GROUP_REGEX = "([0-9]+)([SDT])([*#]?)";
    static final String GROUP_NAME_REGEX = "(?<score>[0-9]+)(?<bonus>[SDT])(?<option>[*#]?)";
    public static void main(String[] args) {
        // ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ์ œ (https://school.programmers.co.kr/learn/courses/30/lessons/17682)

        // 1. ์ƒ์„ฑํ•˜๊ธฐ, ์ •๊ทœ์‹ ์ผ์น˜์—ฌ๋ถ€ ํŒ๋ณ„ํ•˜๊ธฐ
        String input = "hello world";
        System.out.println(Pattern.matches(ALPHA_REGEX, input)); // ๊ณต๋ฐฑ ํฌํ•จ. false

        Pattern pattern = Pattern.compile(ALPHA_REGEX);
        Matcher matcher = pattern.matcher(input);
        System.out.println(matcher.matches()); // ๊ณต๋ฐฑ ํฌํ•จ. false
        System.out.println();

        // 2. Grouping
        // find() - ํŒจํ„ด์ด ์ผ์น˜ํ•˜๋Š” ๋‹ค์Œ ๋ฌธ์ž์—ด์ด ์กด์žฌ๋‹ค๋ฉด true ๋ฐ˜ํ™˜
        pattern = Pattern.compile(GROUP_REGEX);
        matcher = pattern.matcher("1D2S#10S*"); // 1D/2S#/10S*
        while (matcher.find()) {
            System.out.println(matcher.group(1) + "/" + matcher.group(2) + "/" + matcher.group(3));
            System.out.println(matcher.start(1) + "/" + matcher.start(2) + "/" + matcher.start(3));
            System.out.println(matcher.end(1) + "/" + matcher.end(2) + "/" + matcher.end(3));
            System.out.println();
        }

        pattern = Pattern.compile(GROUP_NAME_REGEX);
        matcher = pattern.matcher("1D2S#10S*"); // 1D/2S#/10S*
        while (matcher.find()) {
            System.out.println(matcher.group("score"));
        }
    }
}
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MatcherClassExample2 {
    static final String REGEX = "a*b";
    public static void main(String[] args) {
        String input = "aabzzzaabxxxxabyyyy";

        Pattern pattern = Pattern.compile(REGEX);
        Matcher matcher = pattern.matcher(input);

        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            System.out.println("sb = " + sb);
            System.out.println("matcher.group().toUpperCase() = " + matcher.group().toUpperCase());
            matcher.appendReplacement(sb, matcher.group().toUpperCase());
            System.out.println();
        }
        matcher.appendTail(sb);
        System.out.println("sb = " + sb);
    }
}