How to remove extra spaces from a string in C#

1 Answer

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

class Program
{
    static void Main() {
        string s = "       c#     c  c++        java     ";

        s = s.Trim();
        s = Regex.Replace(s, @"\s+", " ");
        
        Console.Write(s);
    }
}



/*
run:

c# c c++ java

*/

 



answered Jun 15, 2020 by avibootz

Related questions

1 answer 174 views
1 answer 185 views
2 answers 284 views
1 answer 208 views
1 answer 165 views
1 answer 161 views
...