- 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 find a replace a word in a string in C#?
Firstly, set the string to be replaced.
string str = "Demo text!";
Now use the replace() method to replace the above string.
string res = str.Replace("Demo ", "New ");
The following is the complete code to replace a word in a string.
Example
using System; public class Demo { public static void Main() { string str = "Demo text!"; Console.WriteLine(str); string res = str.Replace("Demo ", "New "); Console.WriteLine("After replacing..."); Console.WriteLine(res); } }
Output
Demo text! After replacing... New text!
Advertisements