Replacing substring

In this chapter you will learn:

  1. How to replace substrings
  2. Replace characters

Replace substring

Replace method from string type replaces the found substring with specified string.

using System;/*from   j a  v a  2s. co m*/

class Sample
{
    public static void Main()
    {
        string s = "java2s.com";

        s = s.Replace("java", "JAVA");

        Console.WriteLine(s);

    }
}

The output:

Replace characters

using System;  /*from j a v a 2s .  c o m*/
  
class MainClass {  
  public static void Main() {  
    string str = "This is a test"; 
 
    Console.WriteLine("Original string: " + str); 
     
    // Replace characters 
    str = str.Replace('a', 'X'); 
    Console.WriteLine(str); 
 
  } 
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to remove sub string from a string
Home » C# Tutorial » String
string
String creation
Char in string
Compare strings
String equality
String concatanation
String copy
String Join
String split
String Search for Index
String contains
String start with
String insert
String case
Replacing substring
Remove from a string
Substring
Escape Characters
String verbatim
String padding
Switch on String
String trim
String intern
String normalization
Empty String