- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to replace line breaks in a string in C#?
Let us take we have to eliminate the line breaks, space and tab space from the below string.
eliminate.jpg
Example
We can make use of Replace() extension method of string to do it.
using System; namespace DemoApplication { class Program { static void Main(string[] args) { string testString = "Hello
\r beautiful
\t world"; string replacedValue = testString.Replace("
\r", "_").Replace("
\t", "_"); Console.WriteLine(replacedValue); Console.ReadLine(); } } }
Output
The output of the above code is
Hello _ beautiful _ world
Example
We can also make use of Regex to perform the same operation. Regex is available in System.Text.RegularExpressions namespace.
using System; using System.Text.RegularExpressions; namespace DemoApplication { class Program { static void Main(string[] args) { string testString = "Hello
\r beautiful
\t world"; string replacedValue = Regex.Replace(testString, @"
\r|
\t", "_"); Console.WriteLine(replacedValue); Console.ReadLine(); } } }
Output
The output of the above code is
Hello _ beautiful _ world
Advertisements