string.Replace() / Contains()
| Since: | C# 1.0(2002) |
|---|
The Replace() method replaces a substring, Contains() checks whether a string contains a specified value, and StartsWith() / EndsWith() check whether a string begins or ends with a specified value.
Syntax
string.Replace(string oldValue, string newValue) // Returns true if the string contains value. string.Contains(string value) // Returns true if the string starts with value. string.StartsWith(string value) // Returns true if the string ends with value. string.EndsWith(string value)
Method list
| Method | Description |
|---|---|
| Replace(string oldValue, string newValue) | Returns a new string with all occurrences of oldValue replaced by newValue. |
| Contains(string value) | Returns true if the string contains value; otherwise, false. |
| StartsWith(string value) | Returns true if the string starts with value. |
| EndsWith(string value) | Returns true if the string ends with value. |
Sample Code
Program.cs
using System;
string sentence = "C# is an object-oriented programming language. C# was developed by Microsoft.";
// Use Replace() to substitute all occurrences
string replaced = sentence.Replace("C#", "C-Sharp");
Console.WriteLine(replaced);
// C-Sharp is an object-oriented programming language. C-Sharp was developed by Microsoft.
// Use Contains() to check for a substring
Console.WriteLine(sentence.Contains("Microsoft")); // True
Console.WriteLine(sentence.Contains("Java")); // False
// Use StartsWith() / EndsWith() to check the beginning and end of a string
string url = "https://wp-p.info";
Console.WriteLine(url.StartsWith("https")); // True
Console.WriteLine(url.EndsWith(".info")); // True
Console.WriteLine(url.EndsWith(".com")); // False
This produces the following output:
dotnet run C-Sharp is an object-oriented programming language. C-Sharp was developed by Microsoft. True False True True False
Case-Insensitive Search
By default, these methods are case-sensitive. To compare without case sensitivity, pass StringComparison.OrdinalIgnoreCase.
CaseInsensitive.cs
using System;
string text = "Hello, C# World!";
// Case-sensitive (default)
Console.WriteLine(text.Contains("hello")); // False
Console.WriteLine(text.StartsWith("hello")); // False
// Case-insensitive
Console.WriteLine(text.Contains("hello", StringComparison.OrdinalIgnoreCase)); // True
Console.WriteLine(text.StartsWith("hello", StringComparison.OrdinalIgnoreCase)); // True
// Use Replace() to remove characters by replacing them with an empty string
string noSpaces = "Hello, C# World!".Replace(" ", "");
Console.WriteLine(noSpaces); // Hello,C#World!
This produces the following output:
dotnet run False False True True Hello,C#World!
Practical Pattern: URL Validation
An example combining StartsWith() and Contains() for URL validation.
UrlValidation.cs
using System;
string[] urls = {
"https://wp-p.info",
"http://example.com",
"ftp://files.example.org",
"https://api.example.jp/data"
};
foreach (string url in urls) {
bool isHttps = url.StartsWith("https://");
bool isJpDomain = url.Contains(".jp");
Console.WriteLine($"{url} → HTTPS: {isHttps}, .jp domain: {isJpDomain}");
}
This produces the following output:
dotnet run https://wp-p.info → HTTPS: True, .jp domain: False http://example.com → HTTPS: False, .jp domain: False ftp://files.example.org → HTTPS: False, .jp domain: False https://api.example.jp/data → HTTPS: True, .jp domain: True
Common Mistakes
Common Mistake: Forgetting to Use the Return Value of Replace()
C# strings are immutable. Replace() does not modify the original string — it returns a new one. If you do not assign the return value, the replacement result is lost.
using System;
// NG: The return value of Replace() is not assigned to a variable
string name = "Yagami Iori";
name.Replace("Iori", "Kyo"); // The new string is discarded
Console.WriteLine(name); // Yagami Iori (unchanged)
This produces the following output:
dotnet run Yagami Iori
using System;
// OK: Assign the return value to a variable
string name = "Yagami Iori";
string replaced = name.Replace("Iori", "Kyo");
Console.WriteLine(replaced); // Yagami Kyo
This produces the following output:
dotnet run Yagami Kyo
Notes
All of these methods are case-sensitive. To perform a case-insensitive comparison, pass StringComparison.OrdinalIgnoreCase as an argument, or normalize the string first using ToUpper() / ToLower().
Because C# strings are immutable, Replace() does not modify the original string — it returns a new one. Make sure to assign the return value to a variable.
To split or join strings, see Split() / string.Join().
If you find any errors or copyright issues, please contact us.