Convert Line Breaks To Bars - CSharp System

CSharp examples for System:String New Line

Description

Convert Line Breaks To Bars

Demo Code


using System;/*from  www . j  a  va  2  s . co m*/

public class Main{
        public static string  ConvertLineBreaksToBars (string originalList)
        {
            string[] newLineDelimiters = { Environment.NewLine };
            char[] barDelimiter = { '|' };
            string[] rawItemList = originalList.Split(newLineDelimiters, StringSplitOptions.RemoveEmptyEntries);
            string trimmedItem = "";
            string newList = "";

            foreach (string rawItem in rawItemList)
            {
                trimmedItem = rawItem.Trim();
                if (string.IsNullOrEmpty(trimmedItem)) continue; // ignore blank items
                if (!string.IsNullOrEmpty(newList)) newList += "|"; // Add a '|' if there is already a string in there
                newList += trimmedItem;
            }
            newList = newList.TrimEnd(barDelimiter); //remove the last "|" if items exists
            return newList;
        }
}

Related Tutorials