How To Replace Some Part Of A String In C#


If you are new to C# then you may need this helpful. Lets say there is a scenario where you want to replace some part of a string then here is how to do this in C#.

In this I will show you an example that replaces some part of a HTML template ( string ) in C#.

Code to Replace Some part of a String in C#

Lets declare a string variable and assign it some value as below.
  string replaceMe = "Here I am; And My Name is [[[YourName]]]";
Now the below if block checks if the "replaceMe" string is empty or not and accordingly replacing the Name as "Tapan kumar" in the string and shows it in the label.
 if (!String.IsNullOrEmpty(replaceMe))
     lblShowName.Text = replaceMe.Replace("[[[YourName]]]", "Tapan Kumar");                                                
     lblShowName.Text = "Hello World"; 
Its a very simple thing, I think you may not like this as an article but it may save someones time who is very new to C# coding.

Happy Coding...

No comments:

Post a Comment