C# String Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
Here are some examples on how to replace strings in C#:
To replace a string with another string with case-insensitive matching, you can use the Replace
method with StringComparison
option:
string input = "Hello world!"; string replacement = "bye"; string result = input.Replace("HELLO", replacement, StringComparison.OrdinalIgnoreCase);
In this example, the string "Hello" is replaced with "bye" in the input
string, regardless of the case.
To replace a string in place, you can use the StringBuilder
class:
StringBuilder sb = new StringBuilder("Hello world!"); sb.Replace("Hello", "Hi"); string result = sb.ToString();
In this example, the StringBuilder
class is used to replace the string "Hello" with "Hi" in the sb
object.
To replace only the first occurrence of a string, you can use the Replace
method with a limit parameter:
string input = "Hello world, Hello everyone!"; string replacement = "Hi"; string result = input.Replace("Hello", replacement, 1);
In this example, only the first occurrence of "Hello" is replaced with "Hi" in the input
string.
To replace line breaks with other text, you can use the Replace
method with the Environment.NewLine
constant:
string input = "Hello" + Environment.NewLine + "world!"; string replacement = "<br/>"; string result = input.Replace(Environment.NewLine, replacement);
In this example, the Environment.NewLine
constant is used to represent the line breaks in the input
string, and they are replaced with the <br/>
tag.
To replace multiple line breaks with a single <br/>
tag, you can use a regular expression:
string input = "Hello" + Environment.NewLine + Environment.NewLine + "world!"; string result = Regex.Replace(input, @"(\r\n|\r|\n)+", "<br/>");
In this example, the regular expression (\r\n|\r|\n)+
matches one or more occurrences of line breaks (\r\n
, \r
, or \n
), and replaces them with a single <br/>
tag.
To replace one line break with two line breaks, you can use the Replace
method with a double line break string:
string input = "Hello" + Environment.NewLine + "world!"; string result = input.Replace(Environment.NewLine, Environment.NewLine + Environment.NewLine);
In this example, the Environment.NewLine
constant is used to represent the line breaks in the input
string, and each occurrence is replaced with two line breaks.
To replace multiple spaces with a single space, you can use a regular expression:
string input = "Hello world!"; string result = Regex.Replace(input, @"\s+", " ");
In this example, the regular expression \s+
matches one or more whitespace characters, and replaces them with a single space.
C# replace string example:
string input = "Replace this string"; string oldString = "this"; string newString = "that"; string result = input.Replace(oldString, newString); // Result: "Replace that string"
Replace substring in C# string:
string input = "Replace this substring"; string oldSubstring = "this"; string newSubstring = "that"; string result = input.Replace(oldSubstring, newSubstring); // Result: "Replace that substring"
Replacing characters in C# string:
string input = "Replace characters"; char charToReplace = 'e'; char replacementChar = '*'; string result = input.Replace(charToReplace, replacementChar); // Result: "R*plac* charact*rs"
C# replace multiple occurrences in string:
string input = "Replace multiple multiple occurrences"; string target = "multiple"; string replacement = "single"; string result = input.Replace(target, replacement); // Result: "Replace single single occurrences"
Case-insensitive string replacement in C#:
string input = "Replace case-insensitive"; string target = "CASE"; string replacement = "case"; string result = input.Replace(target, replacement, StringComparison.OrdinalIgnoreCase); // Result: "Replace case-insensitive"
C# regex replace string:
using System.Text.RegularExpressions; string input = "Replace using regex"; string pattern = "using"; string replacement = "with"; string result = Regex.Replace(input, pattern, replacement); // Result: "Replace with regex"
Replace part of a string in C#:
string input = "Replace part of this string"; int startIndex = 12; int length = 4; string replacement = "portion"; string result = input.Remove(startIndex, length).Insert(startIndex, replacement); // Result: "Replace part of portion string"
In-place string replacement in C#:
Replacing part of a string in-place using a StringBuilder
.
StringBuilder sb = new StringBuilder("Replace in-place"); sb.Replace("in-place", "inline"); string result = sb.ToString(); // Result: "Replace inline"
Conditional string replacement in C#: Replacing a substring conditionally.
string input = "Replace conditionally"; string target = "conditionally"; string result = input.Contains(target) ? input.Replace(target, "conditionally replaced") : input; // Result: "Replace conditionally replaced"