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
textstringThe text to process.
whatstringThe string to search for.
replacementstringThe string to replace with.
isAllowingSameWhatAndReplacementboolWhether to allow search and replacement strings to be the same.
isThrowingExIfNotContainsboolWhether 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
textstringThe text to process.
replacementstringThe string to replace with.
searchValuesstring[]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
textstringThe text to process.
replacementstringThe string to replace with.
whatstringThe 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
textstringThe text to process.
replacementstringThe string to replace with.
whatstringThe string to search for.
isPairLinesboolWhether 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
replaceFromIList<string>The list of strings to search for.
replaceToIList<string>The list of replacement strings.
isMultilineWithVariousIndentboolWhether to handle multiline content with different indentation levels.
contentstringThe 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
textstringThe text to process.
replacementstringThe string to replace with.
whatstringThe 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
textstringThe text to process.
replacementstringThe string to replace with.
searchValuesstring[]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
textstringThe text to process.
replacementstringThe string to replace with.
searchValuesstring[]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
textstringThe text to process.
whatstringThe string to search for.
replacementstringThe 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
textstringThe 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
textstringThe text to process.
isAlsoReplacingHtmlboolWhether 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
textstringThe text to process.
isAlsoReplacingHtmlboolWhether 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
textstringThe text to process.
replacementstringThe string to replace with.
whatstringThe string to search for.
forbiddenPrefixstringThe 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
builderStringBuilderThe StringBuilder to modify.
replacementstringThe string to replace with.
searchValuesstring[]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
textstringThe 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
textstringThe text to modify.
replacementstringThe string to insert.
indexintThe starting index of the portion to replace.
lengthintThe 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
builderStringBuilderThe StringBuilder to modify.
replacementstringThe string to insert.
indexintThe starting index of the portion to replace.
lengthintThe 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
textstringThe text to process.
whatstringThe expected first line value.
replacementstringThe 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
textstringThe text to process.
whatstringThe string to search for.
replacementstringThe 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
textstringThe text to process.
replacementstringThe string to replace with.
whatstringThe string to search for.
maxToFirstCharcharThe 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
textstringThe text to process.
replacementstringThe string to replace with.
whatstringThe 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
linesList<string>The list of lines to modify.
lineFromOneintThe 1-based line number to modify.
whatstringThe string to search for.
replacementstringThe string to replace with.
isCheckingForMoreOccurencesboolWhether 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
textstringThe text to process.
whatstringThe string to search for.
replacementstringThe string to replace with.
isCheckingForMoreOccurencesboolWhether 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
textstringThe text to process.
whatstringThe string to search for.
replacementstringThe 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
textstringThe text to process.
mappingTextstringThe mapping text with lines in format "from->to".
isRemovingEndingPairCharsWhenDontHaveStartingboolWhether 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
textstringThe text to process.
mappingTextstringThe mapping text with lines containing from/to pairs separated by the delimiter.
delimiterstringThe 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
textstringThe text to process.
whatstringThe string to search for.
replacementstringThe 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
textstringThe text to modify, passed by reference.
whatstringThe string to search for.
replacementstringThe 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
textstringThe text to process.
whatstringThe regex pattern to search for.
replacementstringThe 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
textstringThe text to process.
isReplacingQuotesboolWhether to replace escaped quotes.
isReplacingT24boolWhether to replace the \t24 escape sequence.
isReplacingBackslashboolWhether 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
builderStringBuilderThe StringBuilder to process.
isReplacingQuotesboolWhether to replace escaped quotes.
isReplacingT24boolWhether to replace the \t24 escape sequence.
isReplacingBackslashboolWhether 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
openCharcharThe character that opens a variable reference.
closeCharcharThe character that closes a variable reference.
innerHtmlstringThe HTML content containing variable references.
dataBindingList<List<string>>The data binding table with variable values.
actualRowintThe 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
innerHtmlstringThe HTML content containing variable references.
dataBindingList<List<string>>The data binding table with variable values.
actualRowintThe 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
textstringThe 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
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
textstringThe 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
textstringThe 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
textstringThe text to process.
replacementstringThe 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
textstringThe text to process.
replacementstringThe 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
textstringThe text to process.
replacementstringThe 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
textstringThe text to process.
whatstringThe string to search for.
replacementstringThe string to replace with.
foundIndexintReference 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
textstringThe text to split.
isReplacingNewLineBySpaceboolWhether to replace newline characters with spaces.
isUsingMoreSpacesForOneboolWhether to collapse multiple spaces into one.
isTrimmingboolWhether to trim each resulting element.
isEscapingQuotationsboolWhether to escape quotation marks.
delimitersstring[]The delimiters to split by.