Remove Extra Space In String In C# Language

Introduction

When you compile and execute below  code , you get replace extra space in your string .

Example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

 

namespace remove_extra_space_in_string                            // use library file

{

    class remove_space                                           //create class

    {

        public void replace(string str1)                         //create function

             {

                 string pattern = "\\s+";   

                 string replacement = " ";                       // replacement pattern

                 Regex rx = new Regex(pattern); 

                 string result = rx.Replace(str1,replacement);  // call to replace space

                 Console.WriteLine("Your string String          : {0}", str1);

                 Console.WriteLine("Remove extra space String   : {0}", result);

                

             }

        static void Main(string[] args)

        {

 

            Console.WriteLine("Enter any string with extra space");

            string readstr = Console.ReadLine();

            remove_space rs = new remove_space();

            rs.replace(readstr);

           

        }

    }

}


Output:


output.jpg


Ebook Download
View all
Learn
View all