How to trim multi spaces from the beginning and ending of a string in C#

2 Answers

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

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

            s = TrimMultiSpaces(s);

            Console.WriteLine("(" + s + ")");
        }
    }
}


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

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

 



answered Jan 31, 2017 by avibootz
0 votes
using System;
using System.Text.RegularExpressions;

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

            s = TrimMultiSpaces(s);

            Console.WriteLine("(" + s + ")");
        }
    }
}


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

 



answered Jan 31, 2017 by avibootz
...