How to remove newlines, carriage returns, multi spaces and tabs from string in C#

1 Answer

0 votes
using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication_C_Sharp
{
    static class Program
    {
        static string ReplaceSpaces(string s)
        {
            return Regex.Replace(s, @"\s+", " ");
        }
        static void Main(string[] args)
        {
            string s = "c#    java  \r\nc        \n c++  \t python  ";

            s = ReplaceSpaces(s);

            Console.WriteLine(s);
        }
    }
}


/*
run:
 
c# java c c++ python
  
*/

 



answered Jan 31, 2017 by avibootz
edited Jan 31, 2017 by avibootz
...