테크매니아
C# : 축자 문자열과 보간된 축자 문자열 본문
반응형
축자 문자열(Verbatim string)
축자 문자열은 Verbatim string이라고 하는데 Verbatim은 '그대로' 라는 뜻을 가지고 있다.C#에서도 Python의 raw string처럼 이스케이프 시퀀스를 무시하고 문자 그대로 출력할 수 있도록 하는 축자 문자열을 제공한다.
Console.WriteLine(@ c:\source\repos
(this is where your code goes));
위에서 보이는 코드처럼 문자열 앞에 에스터리스크를 붙여주면 다음과 같이 이스케이프 시퀀스 문자를 그대로 출력하면서 코드의 분리된 라인까지 그대로 출력한다.출력 결과
c:\source\repos
(this is where your code goes)
보간된 축자 문자열(Interpolated verbatim string)
보간된 축자 문자열은 변수나 수식의 결과를 문자열 {}에 넣고 실행할 수 있도록 해준다. python의 format string과 같은 역할을 한다. 문자열 앞에 달러 기호를 붙이면 된다. 이때 달러와 애스터리스크를 둘다 쓰고 싶을 때는 달러가 먼저 와야 한다.
<pre class="wp-block-code">```csharp
var suffix = this is the end!;
Console.WriteLine($@ c:\source\repos
(this is where your code goes) {suffix});
출력 결과
c:\source\repos
(this is where your code goes) this is the end!
참고 문헌
MSDN 학습 모듈 - C# 문자열 다루기
반응형