문자열 확장은 어떨까요? 업데이트 : 귀하의 질문을 다시 읽고 더 나은 대답이되기를 바랍니다. 이것은 나 또한 버그가 나를 해결할 필요가있는 것인데, 아래와 같이 실망 스럽지만 더 좋은면에서는 효과가있다.
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
public static class StringExtensions
{
public static string StripLeadingWhitespace(this string s)
{
Regex r = new Regex(@"^\s+", RegexOptions.Multiline);
return r.Replace(s, string.Empty);
}
}
}
그리고 예를 들어 콘솔 프로그램 :
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string x = @"This is a test
of the emergency
broadcasting system.";
Console.WriteLine(x);
Console.WriteLine();
Console.WriteLine("---");
Console.WriteLine();
Console.WriteLine(x.StripLeadingWhitespace());
Console.ReadKey();
}
}
}
그리고 출력 :
This is a test
of the emergency
broadcasting system.
---
This is a test
of the emergency
broadcasting system.
그리고 당신은이 경로가는 것을 결정하는 경우에 사용하는 청소기 방법 :
를
string x = @"This is a test
of the emergency
broadcasting system.".StripLeadingWhitespace();
// consider renaming extension to say TrimIndent() or similar if used this way
당신이 예를 제공 할 수 있습니다!? – gideon
내가 일반적으로하는 일은 문자열을 자체 행 (즉,'@ '앞에 줄 바꿈)으로 시작하는 것입니다. 따라서 적어도 오른쪽에서 갑자기 왼쪽에 있지 않습니다. 네가 한 해결책이 아니라는 걸 안다. –
예 나는 종종 그렇게합니다. 그리고 정말로 큰 문제는 아니지만, 이것이 흥미로운 질문이 될 것이라고 생각했습니다. –