Table of Contents

StringReplacePair Struct

Definition

Namespace
Cuemon
Assemblies
Cuemon.Core.dll
Source
src/Cuemon.Core/StringReplacePair.cs

Defines a oldValue/newValue pair that can be set or retrieved for string replace operations.

public readonly struct StringReplacePair

Examples

The following example demonstrates how to use the struct to perform bulk string replacement and removal operations.

using System;
using System.Collections.Generic;
using Cuemon; // for StringReplacePair

namespace MyApp.Examples;

public class StringReplacePairExample
{
    public void Demonstrate()
    {
        string input = "Hello World from Cuemon! Welcome to the World of .NET.";

        // Replace all occurrences of "World" with "Universe" (case-insensitive by default)
        string result = StringReplacePair.ReplaceAll(input, "World", "Universe");
        Console.WriteLine(result);
        // Output: Hello Universe from Cuemon! Welcome to the Universe of .NET.

        // Replace multiple pairs at once
        var pairs = new StringReplacePair[]
        {
            new StringReplacePair("Hello", "Hi"),
            new StringReplacePair("Cuemon", "Codebelt"),
            new StringReplacePair(".NET", "dotnet")
        };
        result = StringReplacePair.ReplaceAll(input, pairs);
        Console.WriteLine(result);
        // Output: Hi Universe from Codebelt! Welcome to the Universe of dotnet.

        // Remove specific words (case-sensitive ordinal comparison)
        result = StringReplacePair.RemoveAll(input, StringComparison.Ordinal, "World", "Cuemon", "Welcome");
        Console.WriteLine(result);
        // Output: Hello  from !  to the  of .NET.

        // Remove specific characters
        result = StringReplacePair.RemoveAll(input, '.', '!');
        Console.WriteLine(result);
        // Output: Hello World from Cuemon Welcome to the World of NET

}
}

Constructors

Name Description
StringReplacePair(string, string)

Initializes a new instance of the StringReplacePair struct.

Properties

Name Description
NewValue

Gets the string value to replace all occurrences of OldValue.

OldValue

Gets the string value to be replaced.

Methods

Name Description
Equals(StringReplacePair)

Indicates whether the current object is equal to another object of the same type.

Equals(object)

Determines whether the specified object is equal to this instance.

GetHashCode()

Returns a hash code for this instance.

RemoveAll(string, params char[])

Returns a new string in which all the specified fragments has been deleted from the specified value.

RemoveAll(string, StringComparison, params string[])

Returns a new string in which all the specified fragments has been deleted from the specified value.

RemoveAll(string, params string[])

Returns a new string in which all the specified fragments has been removed from the specified value.

RemoveAll(string[], StringComparison, params string[])

Returns a new string array in which all the specified fragments has been deleted from the specified source array.

RemoveAll(string[], params string[])

Returns a new string array in which all the specified fragments has been deleted from the specified source array.

ReplaceAll(string, IEnumerable<StringReplacePair>, StringComparison)

Replaces all occurrences of the OldValue with NewValue of the replacePairs sequence in value.

ReplaceAll(string, string, string, StringComparison)

Replaces all occurrences of oldValue in value, with newValue.

ToString()

Returns a string representation of the StringReplacePair, using the string representations of the oldValue and newValue.

Operators

Name Description
operator ==(StringReplacePair, StringReplacePair)

Indicates whether two StringReplacePair instances are equal.

operator !=(StringReplacePair, StringReplacePair)

Indicates whether two DateSpan instances are not equal.