Responsive Advertisement

Java Special Characters: ํŠน์ˆ˜ ๋ฌธ์ž ๋‹ค๋ฃจ๊ธฐ

Java์—์„œ ํŠน์ˆ˜ ๋ฌธ์ž(Special Characters)๋Š” ๋ฌธ์ž์—ด์— ํŠน์ •ํ•œ ํ˜•์‹์„ ์ ์šฉํ•˜๊ฑฐ๋‚˜, ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ํŠน๋ณ„ํ•œ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํŠน์ˆ˜ ๋ฌธ์ž๋Š” ์ฃผ๋กœ ๋ฐฑ์Šฌ๋ž˜์‹œ(\)๋กœ ์‹œ์ž‘ํ•˜๋ฉฐ, ๋ฌธ์ž์—ด ์•ˆ์—์„œ ์ผ๋ฐ˜ ํ…์ŠคํŠธ์™€ ๊ตฌ๋ณ„๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” Java์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ํŠน์ˆ˜ ๋ฌธ์ž์™€ ๊ทธ ํ™œ์šฉ๋ฒ•์„ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1. Java์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์ฃผ์š” ํŠน์ˆ˜ ๋ฌธ์ž

Java๋Š” ๋‹ค์–‘ํ•œ ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ํŠน์ˆ˜ ๋ฌธ์ž์˜ ๋ชฉ๋ก๊ณผ ๊ทธ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค:

ํŠน์ˆ˜ ๋ฌธ์ž ์„ค๋ช… ์˜ˆ์ œ
\n ์ค„ ๋ฐ”๊ฟˆ "Hello\nWorld"
\t ํƒญ ๊ฐ„๊ฒฉ "Hello\tWorld"
\\ ์—ญ์Šฌ๋ž˜์‹œ "C:\\Users\\John"
\' ์ž‘์€ ๋”ฐ์˜ดํ‘œ "It\'s Java"
\" ํฐ ๋”ฐ์˜ดํ‘œ "He said, \"Hello\""
\r ์บ๋ฆฌ์ง€ ๋ฆฌํ„ด "Hello\rWorld"
\b ๋ฐฑ์ŠคํŽ˜์ด์Šค "Hello\bWorld"
\f ํผ ํ”ผ๋“œ "Hello\fWorld"

2. ํŠน์ˆ˜ ๋ฌธ์ž์˜ ์‚ฌ์šฉ ์˜ˆ์ œ

2.1 ์ค„ ๋ฐ”๊ฟˆ๊ณผ ํƒญ

\n์€ ์ค„์„ ๋ฐ”๊ฟ€ ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ, \t๋Š” ํƒญ ๊ฐ„๊ฒฉ์„ ์‚ฝ์ž…ํ•ฉ๋‹ˆ๋‹ค.

public class SpecialCharactersExample {
    public static void main(String[] args) {
        String text = "Hello\nWorld\tJava";
        System.out.println(text);
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

Hello
World   Java

2.2 ์—ญ์Šฌ๋ž˜์‹œ์™€ ๋”ฐ์˜ดํ‘œ

์—ญ์Šฌ๋ž˜์‹œ(\\)์™€ ๋”ฐ์˜ดํ‘œ(\', \")๋Š” ํŒŒ์ผ ๊ฒฝ๋กœ๋‚˜ ๋ฌธ์ž์—ด์—์„œ ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ํ‘œ์‹œํ•˜๋ ค๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

public class PathExample {
    public static void main(String[] args) {
        String filePath = "C:\\Users\\John";
        String quote = "He said, \"Java is fun!\"";

        System.out.println("File Path: " + filePath);
        System.out.println("Quote: " + quote);
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

File Path: C:\Users\John
Quote: He said, "Java is fun!"

2.3 ์บ๋ฆฌ์ง€ ๋ฆฌํ„ด๊ณผ ๋ฐฑ์ŠคํŽ˜์ด์Šค

\r๋Š” ์ปค์„œ๋ฅผ ์ค„์˜ ์ฒ˜์Œ์œผ๋กœ ์ด๋™์‹œํ‚ค๋ฉฐ, \b๋Š” ์ด์ „ ๋ฌธ์ž๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

public class BackspaceExample {
    public static void main(String[] args) {
        String text = "Hello\bWorld!";
        String carriageReturn = "Hello\rWorld";

        System.out.println("๋ฐฑ์ŠคํŽ˜์ด์Šค: " + text);
        System.out.println("์บ๋ฆฌ์ง€ ๋ฆฌํ„ด: " + carriageReturn);
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

๋ฐฑ์ŠคํŽ˜์ด์Šค: HellWorld!
์บ๋ฆฌ์ง€ ๋ฆฌํ„ด: World

3. ํŠน์ˆ˜ ๋ฌธ์ž์™€ ์ด์Šค์ผ€์ดํ”„ ์ฒ˜๋ฆฌ

ํŠน์ˆ˜ ๋ฌธ์ž๋Š” ๋ฌธ์ž์—ด ์•ˆ์—์„œ ์ด์Šค์ผ€์ดํ”„ ์ฒ˜๋ฆฌ๋กœ ๋™์ž‘ํ•˜๋ฉฐ, ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฌธ์ž์—ด์— ํฌํ•จ๋˜๋ฉด ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ํ‘œํ˜„๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž(\)๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ด์Šค์ผ€์ดํ”„ ์ฒ˜๋ฆฌ ์˜ˆ์ œ

public class EscapeExample {
    public static void main(String[] args) {
        String escapedText = "It\'s a \"special\" day!";
        System.out.println(escapedText);
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

It's a "special" day!

4. Unicode์™€ ํŠน์ˆ˜ ๋ฌธ์ž

Java๋Š” ์œ ๋‹ˆ์ฝ”๋“œ(Unicode)๋ฅผ ์ง€์›ํ•˜๋ฏ€๋กœ, ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ์œ ๋‹ˆ์ฝ”๋“œ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ ๋‹ˆ์ฝ”๋“œ๋Š” \uXXXX ํ˜•์‹์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉฐ, XXXX๋Š” 16์ง„์ˆ˜ ๊ฐ’์ž…๋‹ˆ๋‹ค.

Unicode ์‚ฌ์šฉ ์˜ˆ์ œ

public class UnicodeExample {
    public static void main(String[] args) {
        String smiley = "\u263A"; // ์œ ๋‹ˆ์ฝ”๋“œ๋กœ '☺'
        System.out.println("Smiley: " + smiley);
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

Smiley: ☺

5. ํŠน์ˆ˜ ๋ฌธ์ž์™€ ์ •๊ทœ์‹

ํŠน์ˆ˜ ๋ฌธ์ž๋Š” ์ •๊ทœ์‹์—์„œ๋„ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ., *, +, ?๋Š” ์ •๊ทœ์‹์—์„œ ํŠน๋ณ„ํ•œ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ ์ด์Šค์ผ€์ดํ”„ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

์ •๊ทœ์‹์—์„œ ํŠน์ˆ˜ ๋ฌธ์ž ์‚ฌ์šฉ ์˜ˆ์ œ

import java.util.regex.*;

public class RegexExample {
    public static void main(String[] args) {
        String text = "Java is fun.";
        Pattern pattern = Pattern.compile("\\."); // '.' ๋ฌธ์ž ์ฐพ๊ธฐ
        Matcher matcher = pattern.matcher(text);

        if (matcher.find()) {
            System.out.println("ํŠน์ˆ˜ ๋ฌธ์ž '.' ๋ฐœ๊ฒฌ!");
        }
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ:

ํŠน์ˆ˜ ๋ฌธ์ž '.' ๋ฐœ๊ฒฌ!

๊ฒฐ๋ก 

Java์—์„œ ํŠน์ˆ˜ ๋ฌธ์ž๋Š” ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ์™€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ž‘์—…์—์„œ ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ๋กœ ํ™œ์šฉ๋ฉ๋‹ˆ๋‹ค. ํŠน์ˆ˜ ๋ฌธ์ž์™€ ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๋ฅผ ์ดํ•ดํ•˜๋ฉด, ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ์™€ ํฌ๋งทํŒ…, ์ •๊ทœ์‹ ๋“ฑ์„ ๋” ํšจ๊ณผ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ ๋‹ค๋ฃฌ ๋‚ด์šฉ์„ ๋ฐ”ํƒ•์œผ๋กœ ํŠน์ˆ˜ ๋ฌธ์ž๋ฅผ ํ™œ์šฉํ•œ ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ์‹œ๋„ํ•ด ๋ณด์„ธ์š”!

๋Œ“๊ธ€ ์“ฐ๊ธฐ