[ASP.NET] C# 對字串每一行做去空白

VB可以直接用斷行符號分割字串,C#需將斷行符號轉成char再分割

VB可以直接用斷行符號分割字串,C#需將斷行符號轉成char再分割,請參考以下範例:

public static string TrimEachLine(string TrimString)
        {
            StringBuilder sbTrimString = new StringBuilder();
            char DelimiterChar = Environment.NewLine.ToCharArray()[0];
            string[] aryString = TrimString.Split(DelimiterChar);

            foreach (string LineString in aryString)
                sbTrimString.Append(LineString.Trim() + DelimiterChar);

            return sbTrimString.ToString();
        }

後來想到的寫法:

public static string TrimEachLine(string TrimString)
        {
            StringBuilder sbTrimString = new StringBuilder();
            string[] Separators = new string[] { Environment.NewLine };
            string[] aryString = TrimString.Split(Separators, StringSplitOptions.None);

            foreach (string LineString in aryString)
                sbTrimString.Append(LineString.Trim() + Environment.NewLine);

            return sbTrimString.ToString();
        }