Remove New Lines And Spaces - CSharp System

CSharp examples for System:String Strip

Description

Remove New Lines And Spaces

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;

public class Main{
        public static string RemoveNewLinesAndSpaces(this string str)
        {/* w w  w .  j  a  v a  2s. co  m*/
            return str.ExceptChars(new HashSet<char>(new[] {' ', '\t', '\n', '\r'}));
        }
        public static string ExceptChars(this string str, IEnumerable<char> toExclude)
        {
            var sb = new StringBuilder(str.Length);
            for (var i = 0; i < str.Length; i++)
            {
                var c = str[i];
                if (!toExclude.Contains(c))
                    sb.Append(c);
            }
            return sb.ToString();
        }
}

Related Tutorials