Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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