아래 문자열에서 줄 바꿈, 공백 및 탭 공백을 제거해야 한다고 가정해 보겠습니다.
제거.jpg
예시
이를 수행하기 위해 문자열의 Replace() 확장 메서드를 사용할 수 있습니다.
using System;
namespace DemoApplication {
class Program {
static void Main(string[] args) {
string testString = "Hello \n\r beautiful \n\t world";
string replacedValue = testString.Replace("\n\r", "_").Replace("\n\t", "_");
Console.WriteLine(replacedValue);
Console.ReadLine();
}
}
} 출력
위 코드의 출력은
Hello _ beautiful _ world
예시
Regex를 사용하여 동일한 작업을 수행할 수도 있습니다. 정규식은 System.Text.RegularExpressions 네임스페이스에서 사용할 수 있습니다.
using System;
using System.Text.RegularExpressions;
namespace DemoApplication {
class Program {
static void Main(string[] args) {
string testString = "Hello \n\r beautiful \n\t world";
string replacedValue = Regex.Replace(testString, @"\n\r|\n\t", "_");
Console.WriteLine(replacedValue);
Console.ReadLine();
}
}
} 출력
위 코드의 출력은
Hello _ beautiful _ world