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 130 views
1 answer 138 views
2 answers 221 views
1 answer 164 views
1 answer 121 views
1 answer 115 views
...