Table of Contents

Class SHReplace

Namespace
SunamoStringReplace
Assembly
SunamoStringReplace.dll

Provides methods for replacing content within strings.

public class SHReplace
Inheritance
SHReplace
Inherited Members
Extension Methods

Methods

Replace(string, string, string, bool, bool)

Replaces a search string with a replacement string, with optional validation.

public static string Replace(string text, string what, string replacement, bool isAllowingSameWhatAndReplacement = false, bool isThrowingExIfNotContains = false)

Parameters

text string

The text to process.

what string

The string to search for.

replacement string

The string to replace with.

isAllowingSameWhatAndReplacement bool

Whether to allow search and replacement strings to be the same.

isThrowingExIfNotContains bool

Whether to throw an exception if the search string is not found.

Returns

string

The text with the replacement applied.

ReplaceAll(string, string, params string[])

Replaces all occurrences of multiple search strings with a single replacement.

public static string ReplaceAll(string text, string replacement, params string[] searchValues)

Parameters

text string

The text to process.

replacement string

The string to replace with.

searchValues string[]

The strings to search for and replace.

Returns

string

The text with all replacements applied.

ReplaceAll2(string, string, string)

Replaces a single search string with a replacement string in the text.

public static string ReplaceAll2(string text, string replacement, string what)

Parameters

text string

The text to process.

replacement string

The string to replace with.

what string

The string to search for.

Returns

string

The text with the replacement applied.

ReplaceAll2(string, string, string, bool)

Replaces all occurrences of a search string with a replacement, optionally treating input as paired lines.

public static string ReplaceAll2(string text, string replacement, string what, bool isPairLines)

Parameters

text string

The text to process.

replacement string

The string to replace with.

what string

The string to search for.

isPairLines bool

Whether to treat input as paired lines for replacement.

Returns

string

The text with replacements applied.

ReplaceAll3(IList<string>, IList<string>, bool, string)

Replaces multiple search strings with corresponding replacement strings, optionally handling multiline content with various indentation.

public static string ReplaceAll3(IList<string> replaceFrom, IList<string> replaceTo, bool isMultilineWithVariousIndent, string content)

Parameters

replaceFrom IList<string>

The list of strings to search for.

replaceTo IList<string>

The list of replacement strings.

isMultilineWithVariousIndent bool

Whether to handle multiline content with different indentation levels.

content string

The content to process.

Returns

string

The content with all replacements applied.

ReplaceAll4(string, string, string)

Replaces all occurrences of a search string in a loop until none remain.

public static string ReplaceAll4(string text, string replacement, string what)

Parameters

text string

The text to process.

replacement string

The string to replace with.

what string

The string to search for.

Returns

string

The text with all occurrences replaced.

ReplaceAllArray(string, string, params string[])

Replaces all occurrences of multiple search strings with a single replacement. If you want to replace multiline content with various indent use ReplaceAllDoubleSpaceToSingle2 to every variable which you are passed.

public static string ReplaceAllArray(string text, string replacement, params string[] searchValues)

Parameters

text string

The text to process.

replacement string

The string to replace with.

searchValues string[]

The strings to search for and replace.

Returns

string

The text with all replacements applied.

ReplaceAllCaseInsensitive(string, string, params string[])

Replaces all occurrences of search strings in a case-insensitive manner.

public static string ReplaceAllCaseInsensitive(string text, string replacement, params string[] searchValues)

Parameters

text string

The text to process.

replacement string

The string to replace with.

searchValues string[]

The strings to search for and replace.

Returns

string

The text with case-insensitive replacements applied.

ReplaceAllDnArgs(string, string, string)

Replaces all occurrences using swapped argument order for convenience.

public static string ReplaceAllDnArgs(string text, string what, string replacement)

Parameters

text string

The text to process.

what string

The string to search for.

replacement string

The string to replace with.

Returns

string

The text with replacements applied.

ReplaceAllDoubleSpaceToSingle(string)

Replaces all double spaces with single spaces in the given text.

public static string ReplaceAllDoubleSpaceToSingle(string text)

Parameters

text string

The text to process.

Returns

string

The text with all double spaces replaced by single spaces.

ReplaceAllDoubleSpaceToSingle(string, bool)

Replaces all double spaces with single spaces in the given text.

public static string ReplaceAllDoubleSpaceToSingle(string text, bool isAlsoReplacingHtml = false)

Parameters

text string

The text to process.

isAlsoReplacingHtml bool

Whether to also replace HTML non-breaking spaces.

Returns

string

The text with all double spaces replaced by single spaces.

ReplaceAllDoubleSpaceToSingle2(string, bool)

Replaces all double spaces by splitting on whitespace and rejoining with single spaces. Works better than ReplaceAllDoubleSpaceToSingle when copying from webpages.

public static string ReplaceAllDoubleSpaceToSingle2(string text, bool isAlsoReplacingHtml = false)

Parameters

text string

The text to process.

isAlsoReplacingHtml bool

Whether to also replace HTML non-breaking spaces.

Returns

string

The text with all multiple spaces normalized to single spaces.

ReplaceAllExceptPrefixed(string, string, string, string)

Replaces all occurrences of a search string except those prefixed with a specific string.

public static string ReplaceAllExceptPrefixed(string text, string replacement, string what, string forbiddenPrefix)

Parameters

text string

The text to process.

replacement string

The string to replace with.

what string

The string to search for.

forbiddenPrefix string

The prefix that prevents replacement when found before the search string.

Returns

string

The text with non-prefixed occurrences replaced.

ReplaceAllSb(StringBuilder, string, params string[])

Replaces all occurrences of multiple search strings in a StringBuilder with a single replacement.

public static StringBuilder ReplaceAllSb(StringBuilder builder, string replacement, params string[] searchValues)

Parameters

builder StringBuilder

The StringBuilder to modify.

replacement string

The string to replace with.

searchValues string[]

The strings to search for and replace.

Returns

StringBuilder

The modified StringBuilder.

ReplaceAllWhitecharsForSpace(string)

Replaces all whitespace characters with a space character.

public static string ReplaceAllWhitecharsForSpace(string text)

Parameters

text string

The text to process.

Returns

string

The text with all whitespace characters replaced by spaces.

ReplaceByIndex(string, string, int, int)

Replaces a portion of text at a specific index with the specified replacement.

public static string ReplaceByIndex(string text, string replacement, int index, int length)

Parameters

text string

The text to modify.

replacement string

The string to insert.

index int

The starting index of the portion to replace.

length int

The length of the portion to replace.

Returns

string

The modified text.

ReplaceByIndex(StringBuilder, string, int, int)

Replaces a portion of a StringBuilder at a specific index with the specified replacement.

public static StringBuilder ReplaceByIndex(StringBuilder builder, string replacement, int index, int length)

Parameters

builder StringBuilder

The StringBuilder to modify.

replacement string

The string to insert.

index int

The starting index of the portion to replace.

length int

The length of the portion to replace.

Returns

StringBuilder

The modified StringBuilder.

ReplaceFirstLine(string, string, string)

Replaces the first line of the text if it matches the expected value.

public static string ReplaceFirstLine(string text, string what, string replacement)

Parameters

text string

The text to process.

what string

The expected first line value.

replacement string

The string to replace the first line with.

Returns

string

The text with the first line replaced.

ReplaceFirstOccurences(string, string, string)

Replaces the first occurrence of a search string in the text.

public static string ReplaceFirstOccurences(string text, string what, string replacement)

Parameters

text string

The text to process.

what string

The string to search for.

replacement string

The string to replace with.

Returns

string

The text with the first occurrence replaced.

ReplaceFirstOccurences(string, string, string, char)

Replaces the first occurrence of a search string, only if it appears before a specified character.

public static string ReplaceFirstOccurences(string text, string replacement, string what, char maxToFirstChar)

Parameters

text string

The text to process.

replacement string

The string to replace with.

what string

The string to search for.

maxToFirstChar char

The character that limits the search range.

Returns

string

The text with the first occurrence replaced if within range.

ReplaceFromEnd(string, string, string)

Replaces all occurrences of a search string from the end of the text.

public static string ReplaceFromEnd(string text, string replacement, string what)

Parameters

text string

The text to process.

replacement string

The string to replace with.

what string

The string to search for.

Returns

string

The text with replacements applied from end to start.

ReplaceInLine(List<string>, int, string, string, bool)

Replaces a search string in a specific line (1-based) within a list of lines.

public static void ReplaceInLine(List<string> lines, int lineFromOne, string what, string replacement, bool isCheckingForMoreOccurences)

Parameters

lines List<string>

The list of lines to modify.

lineFromOne int

The 1-based line number to modify.

what string

The string to search for.

replacement string

The string to replace with.

isCheckingForMoreOccurences bool

Whether to check for multiple occurrences.

ReplaceInLine(string, string, string, bool)

Replaces a search string in a single line of text.

public static string ReplaceInLine(string text, string what, string replacement, bool isCheckingForMoreOccurences)

Parameters

text string

The text to process.

what string

The string to search for.

replacement string

The string to replace with.

isCheckingForMoreOccurences bool

Whether to check for multiple occurrences.

Returns

string

The text with the replacement applied.

ReplaceLastOccurenceOfString(string, string, string)

Replaces the last occurrence of a search string in the text.

public static string ReplaceLastOccurenceOfString(string text, string what, string replacement)

Parameters

text string

The text to process.

what string

The string to search for.

replacement string

The string to replace with.

Returns

string

The text with the last occurrence replaced.

ReplaceMany(string, string, bool)

Replaces content in the input text using a multiline mapping string with arrow notation.

public static string ReplaceMany(string text, string mappingText, bool isRemovingEndingPairCharsWhenDontHaveStarting = true)

Parameters

text string

The text to process.

mappingText string

The mapping text with lines in format "from->to".

isRemovingEndingPairCharsWhenDontHaveStarting bool

Whether to remove unmatched ending pair characters.

Returns

string

The text with all mapped replacements applied.

ReplaceManyFromString(string, string, string)

Replaces content in the text using a multiline mapping string with a custom delimiter.

public static string ReplaceManyFromString(string text, string mappingText, string delimiter)

Parameters

text string

The text to process.

mappingText string

The mapping text with lines containing from/to pairs separated by the delimiter.

delimiter string

The delimiter separating from and to values in each line.

Returns

string

The text with all mapped replacements applied.

ReplaceOnce(string, string, string)

Replaces only the first occurrence of a search string in the text.

public static string ReplaceOnce(string text, string what, string replacement)

Parameters

text string

The text to process.

what string

The string to search for.

replacement string

The string to replace with.

Returns

string

The text with the first occurrence replaced.

ReplaceRef(ref string, string, string)

Replaces occurrences of a search string in the text passed by reference.

public static string ReplaceRef(ref string text, string what, string replacement)

Parameters

text string

The text to modify, passed by reference.

what string

The string to search for.

replacement string

The string to replace with.

Returns

string

The modified text.

ReplaceSecondAndNextOccurencesOfStringFrom(string, string, string)

Replaces the second and subsequent occurrences of a pattern in the text.

public static string ReplaceSecondAndNextOccurencesOfStringFrom(string text, string what, string replacement)

Parameters

text string

The text to process.

what string

The regex pattern to search for.

replacement string

The string to replace with.

Returns

string

The text with second and subsequent occurrences replaced.

ReplaceTypedWhitespacesForNormal(string, bool, bool, bool)

Replaces typed whitespace escape sequences with their actual character equivalents.

public static string ReplaceTypedWhitespacesForNormal(string text, bool isReplacingQuotes, bool isReplacingT24, bool isReplacingBackslash)

Parameters

text string

The text to process.

isReplacingQuotes bool

Whether to replace escaped quotes.

isReplacingT24 bool

Whether to replace the \t24 escape sequence.

isReplacingBackslash bool

Whether to replace escaped backslashes.

Returns

string

The text with escape sequences replaced by actual characters.

ReplaceTypedWhitespacesForNormal(StringBuilder, bool, bool, bool)

Replaces typed whitespace escape sequences with their actual character equivalents in a StringBuilder.

public static StringBuilder ReplaceTypedWhitespacesForNormal(StringBuilder builder, bool isReplacingQuotes, bool isReplacingT24, bool isReplacingBackslash)

Parameters

builder StringBuilder

The StringBuilder to process.

isReplacingQuotes bool

Whether to replace escaped quotes.

isReplacingT24 bool

Whether to replace the \t24 escape sequence.

isReplacingBackslash bool

Whether to replace escaped backslashes.

Returns

StringBuilder

The StringBuilder with escape sequences replaced by actual characters.

ReplaceVariables(char, char, string, List<List<string>>, int)

Replaces template variables in HTML content with values from a data binding table.

public static string ReplaceVariables(char openChar, char closeChar, string innerHtml, List<List<string>> dataBinding, int actualRow)

Parameters

openChar char

The character that opens a variable reference.

closeChar char

The character that closes a variable reference.

innerHtml string

The HTML content containing variable references.

dataBinding List<List<string>>

The data binding table with variable values.

actualRow int

The row index to use for variable values.

Returns

string

The HTML content with variables replaced by their values.

ReplaceVariables(string, List<List<string>>, int)

Replaces template variables enclosed in curly braces in HTML content.

public static string ReplaceVariables(string innerHtml, List<List<string>> dataBinding, int actualRow)

Parameters

innerHtml string

The HTML content containing variable references.

dataBinding List<List<string>>

The data binding table with variable values.

actualRow int

The row index to use for variable values.

Returns

string

The HTML content with variables replaced by their values.

ReplaceWhiteSpaces(string)

Replaces every whitespace character with an empty string.

public static string ReplaceWhiteSpaces(string text)

Parameters

text string

The text to process.

Returns

string

The text with all whitespace characters removed.

ReplaceWhiteSpaces(string, string)

Replaces all whitespace characters except spaces first, then replaces the result with the specified replacement.

public static string ReplaceWhiteSpaces(string text, string replacement)

Parameters

text string

The text to process.

replacement string

The string to replace whitespace with.

Returns

string

The text with whitespace characters replaced.

ReplaceWhiteSpacesAndTrim(string)

Replaces whitespace characters in the text and trims the result.

public static string ReplaceWhiteSpacesAndTrim(string text)

Parameters

text string

The text to process.

Returns

string

The text with whitespace characters replaced and trimmed.

ReplaceWhiteSpacesExcludeSpaces(string)

Removes whitespace characters (carriage return, newline, tab) from the text, excluding spaces.

public static string ReplaceWhiteSpacesExcludeSpaces(string text)

Parameters

text string

The text to process.

Returns

string

The text with non-space whitespace characters removed.

ReplaceWhiteSpacesWithoutSpaces(string, string)

Replaces whitespace characters (excluding spaces) with the specified replacement.

public static string ReplaceWhiteSpacesWithoutSpaces(string text, string replacement)

Parameters

text string

The text to process.

replacement string

The string to replace whitespace characters with.

Returns

string

The text with whitespace characters (except spaces) replaced.

ReplaceWhiteSpacesWithoutSpacesWithReplaceWith(string, string)

Replaces carriage return, newline, and tab characters with the specified replacement.

public static string ReplaceWhiteSpacesWithoutSpacesWithReplaceWith(string text, string replacement)

Parameters

text string

The text to process.

replacement string

The string to replace whitespace characters with.

Returns

string

The text with carriage return, newline, and tab characters replaced.

ReplaceWhitespaces(string, string)

Replaces all whitespace characters in the text with the specified replacement.

public static string ReplaceWhitespaces(string text, string replacement)

Parameters

text string

The text to process.

replacement string

The string to replace whitespace characters with.

Returns

string

The text with all whitespace characters replaced.

ReplaceWithIndex(string, string, string, ref int)

Replaces the first occurrence of a search string at a tracked index position.

public static string ReplaceWithIndex(string text, string what, string replacement, ref int foundIndex)

Parameters

text string

The text to process.

what string

The string to search for.

replacement string

The string to replace with.

foundIndex int

Reference to the index where the replacement occurred; -1 if not yet found.

Returns

string

The text with the replacement applied.

SplitAdvanced(string, bool, bool, bool, bool, params string[])

Splits a string using advanced options such as replacing newlines, trimming, and escaping quotations.

public static List<string> SplitAdvanced(string text, bool isReplacingNewLineBySpace, bool isUsingMoreSpacesForOne, bool isTrimming, bool isEscapingQuotations, params string[] delimiters)

Parameters

text string

The text to split.

isReplacingNewLineBySpace bool

Whether to replace newline characters with spaces.

isUsingMoreSpacesForOne bool

Whether to collapse multiple spaces into one.

isTrimming bool

Whether to trim each resulting element.

isEscapingQuotations bool

Whether to escape quotation marks.

delimiters string[]

The delimiters to split by.

Returns

List<string>

A list of split string elements.