Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
C# Program to remove whitespaces in a string
Let’s say the following is the string −
StringBuilder str = new StringBuilder("Patience is key!");
To remove whitespace, you can use the replace method.
str.Replace(" ", "");
Let us see the complete code.
Example
using System;
using System.Text;
class Demo {
static void Main() {
// Initial String
StringBuilder str = new StringBuilder("Patience is key!");
Console.WriteLine(str.ToString());
// Replace
str.Replace(" ", "");
// New String
Console.WriteLine(str.ToString());
Console.ReadLine();
}
}
Output
Patience is key! Patienceiskey!
Advertisements
