
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
Removing Whitespaces Using C# Regex
Let’s say we want to remove whitespace from the following string str1.
string str1 = "Brad Pitt";
Now, use Regex Replace to replace whitespace with empty. Here, we have used System.Text.RegularExpressions.
string str2 = System.Text.RegularExpressions.Regex.Replace(str1, @"\s+", "");
Let us see the complete example.
Example
using System; using System.Text.RegularExpressions; namespace Demo { class Program { static void Main(string[] args) { string str1 = "Brad Pitt"; Console.WriteLine(str1); string str2 = System.Text.RegularExpressions.Regex.Replace(str1, @"\s+", ""); Console.WriteLine(str2); } } }
Output
Brad Pitt BradPitt
Advertisements