
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to replace multiple spaces with a single space in C#?
There are several ways to replace multiple spaces with single space in C#.
String.Replace − Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String.
Replace(String, String, Boolean, CultureInfo)
String.Join Concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member.
Regex.Replace −In a specified input string, replaces strings that match a regular expression pattern with a specified replacement string.
Example using Regex −
Example
using System; using System.Text.RegularExpressions; namespace DemoApplication{ class Program{ public static void Main(){ string stringWithMulipleSpaces = "Hello World. Hi Everyone"; Console.WriteLine($"String with multiples spaces: {stringWithMulipleSpaces}"); string stringWithSingleSpace = Regex.Replace(stringWithMulipleSpaces, @"\s+", " "); Console.WriteLine($"String with single space: {stringWithSingleSpace}"); Console.ReadLine(); } } }
Output
The output of the above program is
String with multiples spaces: Hello World. Hi Everyone String with single space: Hello World. Hi Everyone
In the above example Regex.Replace we have identified the additional spaces and replaced with single space
Example using string.Join −
Example
using System; namespace DemoApplication{ class Program{ public static void Main(){ string stringWithMulipleSpaces = "Hello World. Hi Everyone"; Console.WriteLine($"String with multiples spaces: {stringWithMulipleSpaces}"); string stringWithSingleSpace = string.Join(" ", stringWithMulipleSpaces.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); Console.WriteLine($"String with single space: {stringWithSingleSpace}"); Console.ReadLine(); } } }
Output
The output of the above program is
String with multiples spaces: Hello World. Hi Everyone String with single space: Hello World. Hi Everyone
In the above we are splitting the text with multiple spaces using Split method and later join the splitted array using the Join method with single space.
- Related Articles
- How to replace multiple spaces in a string using a single space using Java regex?
- How to replace leading zero with spaces - JavaScript
- How can I replace newlines with spaces in JavaScript?
- C# program to replace all spaces in a string with ‘%20’
- Capitalize a word and replace spaces with underscore - JavaScript?
- Replace Tab with space in SAP ABAP
- How do I replace “+”(plus sign) with SPACE in MySQL?
- Golang program to replace the spaces of string with a specific character
- How to sort multiple columns with a single query?\n
- Java Program to Replace the Spaces of a String with a Specific Character
- How to replace missing values recorded with blank spaces in R with NA or any other value?
- How to insert multiple rows with single MySQL query?
- How to expand tabs in string to multiple spaces in Python?
- How to transform two or more spaces in a string in only one space? JavaScript
- Python Program to Take in a String and Replace Every Blank Space with Hyphen
