Programming/Kotlin

[Kotlin] ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž (escape character) ์™€ RAW ๋ฌธ์ž์—ด

yuri lee 2022. 10. 5. 08:14
๋ฐ˜์‘ํ˜•

Intro

์ฝ”ํ‹€๋ฆฐ์—์„œ์˜ ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž (escape character), ๊ทธ๋ฆฌ๊ณ  Raw์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

Escape character

์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๋ž€, ๋ฌธ์ž์—ด ๋‚ด์—์„œ ํŠน์ˆ˜ํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฌธ์ž์ž…๋‹ˆ๋‹ค. ์—ญ์Šฌ๋ž˜์‹œ(\) ๊ธฐํ˜ธ๊ฐ€ ๋ถ™์€ ํŠน์ˆ˜ํ•œ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๋ฌธ์ž ๋ฆฌํ„ฐ๋Ÿด ์ž…๋‹ˆ๋‹ค. ์ฆ‰ ๋ฌธ์ž์—ด ์•ˆ์—์„œ ํŠน๋ณ„ํ•œ ์˜๋ฏธ๋กœ ํ•ด์„๋˜๋Š” ๋ฌธ์ž์ž…๋‹ˆ๋‹ค. 

 

๊ตฌ๋ถ„ ์˜๋ฏธ
\a ๊ฒฝ๊ณ ์Œ ์ถœ๋ ฅ
\n ์ค„ ๋ฐ”๊ฟˆ, ๊ฐœํ–‰ (new line)
\t ์ˆ˜ํ‰ ํƒญ์œผ๋กœ ์ผ์ •ํ•œ ๊ฐ€๊ฒฉ ๋„์šฐ๊ธฐ, ํƒญ (tab)
\b ๋ฐฑ์ŠคํŽ˜์ด์Šค๋กœ ๋’ค๋กœ ํ•œ ์นธ ์ด๋™ (backspace)
\r ๋™์ผํ•œ ์ค„์˜ ๋งจ ์•ž ์นธ์œผ๋กœ ์ปค์„œ ์ด๋™ (carriage return)
\f ์ถœ๋ ฅ ์šฉ์ง€๋ฅผ ํ•œ ํŽ˜์ด์ง€ ๋„˜๊น€ (form feed)
\\ \(์—ญ์Šฌ๋ž˜์‹œ) ๋ฌธ์ž ์ถœ๋ ฅ
\' '์ž‘์€ ๋”ฐ์Œํ‘œ ๋ฌธ์ž ์ถœ๋ ฅ (single quote)
\" " ํฐ ๋”ฐ์Œํ‘œ ๋ฌธ์ž ์ถœ๋ ฅ (double quote)
\0 ๋„ ๋ฌธ์ž ์ถœ๋ ฅ (null)

 

RAW ๋ฌธ์ž์—ด

์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ์ง€์ €๋ถ„ํ•ด์ง‘๋‹ˆ๋‹ค. ์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹  ์‹œ์ž‘๊ณผ ๋์—์„œ ํฐ๋”ฐ์˜ดํ‘œ๋ฅผ ์„ธ ๊ฐœ๋ฅผ ์ด์šฉํ•ด raw ๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 

// input
fun main() {    
    val name = "Eve"
    val memo = """Dear $name, a quick reminder about the
    ~party we have scheduled next Tuesday at
    ~the 'Low Cermony Cafe' at Noon. | Please plan to..."""
    println(memo.trimMargin("~"))
}

// output
Dear Eve, a quick reminder about the
party we have scheduled next Tuesday at
the 'Low Cermony Cafe' at Noon. | Please plan to...

๋ฌผ๊ฒฐํ‘œ(~)๋ฅผ ๊ตฌ๋ถ„์ ์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฉ€ํ‹ฐ๋ผ์ธ ๋ฌธ์ž์—ด์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 


https://codedragon.tistory.com/482

๋ฐ˜์‘ํ˜•