文字列拡張はどうですか?更新:私はあなたの質問を読んで、私は良い答えがあることを願っています。これは私にもバグであり、以下のようにそれを解決しなければならないことはイライラしていますが、プラス側ではうまくいきます。
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
私が普通にやっているのは、文字列を独自の行(つまり、 '@'の前の改行文字)で始めることです。そのため、少なくとも右は突然左にありません。私はそれがあなたが後にしている解決策ではないことを知っています。 –
はい、私はしばしばそうします。本当に大したことではありませんが、これは面白い質問です。 –