Logo

Programming-Idioms

History of Idiom 63 > diff from v51 to v52

Edit summary for version 52 by zqwnvl:
[C#] Follow best practices; add links

Version 51

2021-08-19, 06:32:40

Version 52

2021-08-19, 06:38:09

Idiom #63 Replace fragment of a string

Assign to x2 the value of string x with all occurrences of y replaced by z.
Assume occurrences of y are not overlapping.

Idiom #63 Replace fragment of a string

Assign to x2 the value of string x with all occurrences of y replaced by z.
Assume occurrences of y are not overlapping.

Variables
x2,x,y,z
Variables
x2,x,y,z
Extra Keywords
substring substitute
Extra Keywords
substring substitute
Imports
Imports
using System;
Code
string x2 = x.Replace(y, z);
Code
string x2 = x.Replace(y, z, StringComparison.Ordinal);
Comments bubble
It is best practice to explicitly specify the string comparison method when working with strings in .NET.
Doc URL
https://docs.microsoft.com/en-us/dotnet/standard/base-types/best-practices-strings
Origin
https://docs.microsoft.com/en-us/dotnet/api/system.string.replace
Demo URL
https://sharplab.io/#gist:5d1f35427c99737bee52d6e056bf7f7d