날마다 조금씩 Swift - 문자열편1

유진혁·2023년 10월 18일
0

swift

목록 보기
10/17

String Literals

다른 언어들과 같이 큰 따옴표("")로 감싸서 코드 내에 미리 정의된 문자열을 넣을 수 있다.

let someString = "Some string literal value"

 

Multiline String Literals

여러 줄의 문자열을 표기하고 싶을 땐 큰따옴표를 3개 겹쳐 사용한다.

let quotation = """
The White Rabbit put on his spectacles.  "Where shall I begin,
please your Majesty?" he asked.

"Begin at the beginning," the King said gravely, "and go on
till you come to the end; then stop."
"""

줄바꿈 되었을 때만 이를 평가하기 때문에 아래와 같이 쓸 경우 같은 값을 담고 있다.

let singleLineString = "These are the same."
let multilineString = """
These are the same.
"""

코드를 읽기 쉽게하기 위해 줄바꿈을 넣고싶지만, String 값에는 줄바꿈을 넣고 싶지 않을 때는 백슬래쉬(\)를 사용한다

let softWrappedQuotation = """
The White Rabbit put on his spectacles.  "Where shall I begin, \
please your Majesty?" he asked.

"Begin at the beginning," the King said gravely, "and go on \
till you come to the end; then stop."
"""

여러줄의 문자열이 줄바꿈으로 시작되거나 끝나게 하고싶을 경우에는 앞뒤로 빈 줄을 넣어주면 된다.

let lineBreaks = """

This string starts with a line break.
It also ends with a line break.

"""

"""전의 공백들은 무시되지만, 공백을 넣고싶을 때는 아래와같이 indent의 차이를 두면 된다.

profile
개발자

0개의 댓글