StringExtensionMethods

public static class StringExtensionMethods
Extension methods for System.String

Methods

Try and break a string into a fixed number of fragments at custom characters.
public static string[] Break(
this string
text,
String to break.
Font
font,
Font to use for measuring.
fragments,
Target number of fragments.
char[]
leadingBreaks = null,
Spitting characters. Splits will be made following these characters.
char[]
trailingBreaks = null
Spitting characters. Splits will be made preceding these characters.
)
Array of fragments. May or may not be as long as the fragments integer.

CollapseWhiteSpace

#Example
Collapse all whitespace runs with a single symbol.
public static string CollapseWhiteSpace(
this string
text,
String to modify.
symbol = '\0'
Symbol to insert for a white-space run. Leave at 'null' to not insert any symbol.
)
Collapsed string.

CommonOverlap

#Example
Gets the string which is the common overlap (at the start) of both strings.
public static string CommonOverlap(
this string
a,
b
)

CompressedStringToGuid

#Example
Convert a short string representation into a Guid.
public static Guid CompressedStringToGuid(
this string
data
String represtation.
)
Guid.

Consume

#Example
Consume up to a specific number of characters from the start of the string.
public static string Consume(
this string
text,
String to consume.
out string
remainder,
What's left of the string.
characters
Number of characters to consume.
)
Consumed characters.

Consume

#Example
Consume a set of characters from the start of the string until another character appears.
public static string Consume(
this string
text,
String to consume.
out string
remainder,
What's left of the string.
params char[]
characters
Characters to consume.
)
Consumed characters.

ConsumeUntil

#Example
Consume a set of characters from the start of the string until a specified character appears.
public static string ConsumeUntil(
this string
text,
String to consume.
out string
remainder,
What's left of the string.
params char[]
characters
Characters to consume.
)
Consumed characters.

Contains

#Example
Perform advanced containment test.
public static bool Contains(
this string
text,
search,
comparison
)

ContainsIsolated

#Example
Test to see whether a string contains another string, surrounded by non-alphanumeric characters.
public static bool ContainsIsolated(
this string
text,
String to search.
pattern,
String to search for.
startIndex,
First index to include in the search.
comparisonType
Comparison to use.
)
True if the string contains the substring.

ContainsIsolated

#Example
Test to see whether a string contains another string, surrounded by non-alphanumeric characters.
public static bool ContainsIsolated(
this string
text,
String to search.
pattern,
String to search for.
comparisonType
Comparison to use.
)
True if the string contains the substring.
Count the number of times a specific character occurs in this string.
public static int Count(
this string
text,
String.
character
Character to search for.
)
The number of occurrences of the character in the string.
Count the number of times a specific string occurs in this string.
public static int Count(
this string
text,
String
segment,
Fragment to search for.
compare
Comparison.
)
The number of occurrences of the fragment in this string.
Count the number of times a specific string occurs in this string.
public static int Count(
this string
text,
String
segment
Fragment to search for.
)
The number of occurrences of the fragment in this string.

DecodeEscapedCharacters

#Example
Decode escaping of special characters like '¶', '\t', '\n' and unicode codepoints: '\u00A0'.
public static string DecodeEscapedCharacters(
this string
text,
arrows = true,
ellipses = true,
pilcrows = true,
slashTabs = true,
slashBreaks = true,
slashCodepoints = true
)

DecodeWhitespace

#Example
Decode (i.e. un-escape) various kinds of non-whitespace characters using whitespace characters.
public static string DecodeWhitespace(
this string
text,
Text to encode.
types
White space types to decode.
)
Decoded string.

DropSuperScript

#Example
Convert a superscript character into a regular character.
public static char DropSuperScript(
this char
c
)

EncodeWhitespace

#Example
Encode (i.e. escape) various kinds of whitespace characters using non-whitespace characters.
public static string EncodeWhitespace(
this string
text,
Text to encode.
types
White space types to encode.
)
Encoded string.

EndsWithBeforeWhitespace

#Example
Test whether a string ends with a given character, ignoring trialing whitespace.
public static bool EndsWithBeforeWhitespace(
this string
text,
character
)

EqualsAny

#Example
Check whether a string is equal to any one of a set of strings.
public static bool EqualsAny(
this string
text,
String to compare.
comparison,
Comparison type.
params string[]
matches
Set of possible matches.
)
True if string is equal to at least one of the matches.
Finds all the occurrences of a character within a substring.
public static int[] Find(
this string
text,
String.
character,
Character to search for.
mask,
String mask.
firstChar,
Index of first character to take into account.
lastChar
Index of last character to take into account.
)
An array of integers describing the positions of the given character within this string.
InvalidOperationExceptionThrown when braces, brackets or parenthesis are filtered yet there are no correctly matching pairs.
Finds all the occurrences of a character within this string.
public static int[] Find(
this string
text,
String.
character,
Character to search for.
mask
String mask.
)
An array of integers describing the positions of the given character within this string.
InvalidOperationExceptionThrown when braces, brackets or parenthesis are filtered yet there are no correctly matching pairs.

FindAll

#Example
Find all occurrences of a specific set of characters.
public static int[] FindAll(
this string
text,
String to search.
filter,
Filtering mask.
params char[]
characters
Characters to search for.
)
Array of character indices.

FindAll

#Example
Find all occurrences of a specific set of characters.
public static int[] FindAll(
this string
text,
String to search.
params char[]
characters
Characters to search for.
)
Array of character indices.

FindIsolated

#Example
Find the first isolated occurence of one string within another. Isolated in this case means not surrounded on either side by alphanumeric characters or underscores.
public static int FindIsolated(
this string
text,
String to search.
pattern,
String to search for.
startIndex,
First index to include in the search.
comparisonType
Comparison to use.
)
Index at which isolated substring occurs or -1 if no isolated string could be found.

FindIsolated

#Example
Find the first isolated occurence of one string within another. Isolated in this case means not surrounded on either side by alphanumeric characters or underscores.
public static int FindIsolated(
this string
text,
String to search.
pattern,
String to search for.
comparisonType
Comparison to use.
)
Index at which isolated substring occurs or -1 if no isolated string could be found.

FindLineStart

#Example
Find the index within the string where the line is said to start. If we're past the leading whitespace of the line, this will return the index of the last char in the leading whitespace. If we're already in the leading whitespace, zero is returned.
public static int FindLineStart(
this string
text,
caret
)

FindMatchingBrace

#Example
Find a matching brace pair.
public static int FindMatchingBrace(
this string
text,
String to search.
start,
start index of search. If the character at text[start] equals the open symbol, it is considered to be part of a nested pair, so be sure to start your search *after* the opening brace of the pair you care about.
open = '(',
Open brace symbol.
close = ')'
Close brace symbol.
)
Index of the matching closing brace string, or a negative value indicating the nesting depth when the string ran out of characters.

FindNext

#Example
Find the next occurance of a specific character in this string.
public static int FindNext(
this string
text,
String.
character,
Character to search for.
mask,
Searching options.
startChar
)
The index of the first character that matches the search char, or -1 if no such char could be found.

FindSymbolRegion

#Example
Starting from the character at text[seed], find all identical adjacent characters.
public static (int start, int end) FindSymbolRegion(
this string
text,
Text to search.
seed
Seed character index.
)
First and last index of the region contiguous with the seed character.

FindWhiteRegion

#Example
Starting from the character at text[seed], find all adjacent whitespace in either direction.
public static (int start, int end) FindWhiteRegion(
this string
text,
Text to search.
seed
Seed character index.
)
First and last index of the whitespace region contiguous with the seed character.

FindWordRegion

#Example
Starting from the character at text[seed], find all adjacent word or numberlike characters in either direction.
public static (int start, int end) FindWordRegion(
this string
text,
Text to search.
seed
Seed character index.
)
First and last index of the word/number region contiguous with the seed character.

GraphemeCount

#Example
Gets the number of grapheme clusters in the text.
public static int GraphemeCount(
this string
text
)

GraphemeToCodeUnit

#Example
Converts a grapheme-cluster index into the code-unit offset at which that cluster begins (clamped to the string length).
public static int GraphemeToCodeUnit(
this string
text,
graphemeIndex
)

IsHexDigit

#Example
Test whether a char qualifies as a hexadecimal digit.
public static bool IsHexDigit(
this char
c
)

IsSingleLineString

#Example
Gets whether a string is single or multi-line.
public static bool IsSingleLineString(
this string
text
String to test.
)
True if the string doesn't contain any linefeeds or newline characters.

NextTransition

#Example
Find the index within the string where the next meaningful transition occurs.
public static int NextTransition(
this string
text,
caret
)

OffsetAndAlign

#Example
Offset an array of strings by inserting whitespace at the start until the specified characters are all as close as possible to left aligned.
public static string[] OffsetAndAlign(
this string[]
lines,
Lines to align.
charIndex,
Index of character for left-alignment.
Font
font
Font to measure with.
)
Possibly modified array of strings.

OffsetAndAlign

#Example
Offset an array of strings by inserting whitespace at the start until the specified characters are all as close as possible to left aligned.
public static string[] OffsetAndAlign(
this string[]
lines,
Lines to align.
int[]
charIndices,
Index to align per line.
Font
font
Font to measure with.
)

OffsetAndAlign

#Example
Offset an array of strings by inserting whitespace at the start until the specified characters are all as close as possible to left aligned.
public static string[] OffsetAndAlign(
this string[]
lines,
Lines to align.
marker,
Marker inside string to align.
Font
font
Font to measure with.
)
Possibly modified array of strings.

PadToWidest

#Example
Append whitespace to the end of lines until they're all as long as the longest one.
public static string[] PadToWidest(
this string[]
lines,
Font
font
)

PrevTransition

#Example
Find the index within the string where the previous meaningful transition occurs.
public static int PrevTransition(
this string
text,
caret
)

RemoveAll

#Example
Remove all occurrences of a set of characters.
public static string RemoveAll(
this string
text,
params char[]
characters
)

RemoveAll

#Example
Remove all occurrences of a substring, with a string comparison type.
public static string RemoveAll(
this string
text,
search,
comparison
)

RemoveAll

#Example
Remove all occurrences of a substring, with a string comparison type.
public static string RemoveAll(
this string
text,
search,
comparison
)

RemoveSurroundingBraces

#Example
Remove matching braces which delimit a string.
public static string RemoveSurroundingBraces(
this string
text,
Text to modify.
open = '(',
Opening brace.
close = ')',
Closing brace.
replacement = null
Optional replacement.
)

RemoveWhiteSpace

#Example
Remove all whitespace from a string.
public static string RemoveWhiteSpace(
this string
text,
String to modify.
allowedRunLength
Leave this many of a consecutive sequence of identical whitespace characters.
)
Modified string.

RemoveWhiteSpace

#Example
Remove all whitespace from a string.
public static string RemoveWhiteSpace(
this string
text
String to modify.
)
Modified string.

Replace

#Example
Replace all characters with a single other character.
public static string Replace(
this string
text,
String to modify.
search,
Characters to search for.
replace
Replacement.
)
Modified string.

Replace

#Example
Replace all characters with a single other character.
public static string Replace(
this string
text,
String to modify.
search,
Characters to search for.
replace
Replacement.
)
Modified string.

Replace

#Example
Replace all occurrences of one string within another.
public static string Replace(
this string
text,
String to operate on.
search,
String to replace.
replace,
Replacement string.
mask,
Replacement mask. Only replacements fully inside the mask will be made.
comparison,
Comparison to use.
startIndex
Character position for replacement start.
)
Modified string.

Replace

#Example
Replace all occurrences of one string within another.
public static string Replace(
this string
text,
String to operate on.
search,
String to replace.
replace,
Replacement string.
mask,
Replacement mask. Only replacements fully inside the mask will be made.
comparison
Comparison to use.
)
Modified string.

Replace

#Example
Replace all occurences of some string with another.
public static string Replace(
this string
text,
String to modify.
search,
Search keys.
replace,
Replacement values.
culture,
Culture with string matching rules.
out int
replacements
Number of replacements made.
)
Modified string.

ReplaceRepeatedly

#Example
Replace a set of characters with another until the string no longer changes.
public static string ReplaceRepeatedly(
this string
text,
Text to modify.
search,
Search pattern.
replace
Replace pattern.
)

SimplifyNewLines

#Example
Reduce all newline codes in a string and replace them with a single new code.
public static string SimplifyNewLines(
this string
text,
Text to simplify.
newLineString = "\n"
Newline code to use.
)

SplitAtChar

#Example
Split a string using repetitions of a specific character.
public static string[] SplitAtChar(
this string
text,
String to split.
symbol,
Splitting character.
minRepeat
Minimum number of repetitions of symbol before it splits.
)
Array of string fragments.

SplitAtIndex

#Example
Split a string into several segments based on splitting indices.
public static string[] SplitAtIndex(
this string
text,
String to split.
splitIndices,
Indices to split at. Indices will be sorted and coincident indices will be ignored.
options
Splitting options.
)
An array of split segments.

SplitLines

#Example
Split a string into individual lines.
public static string[] SplitLines(
this string
text
String to split.
)
Array of lines.

StartsWithAfterWhitespace

#Example
Test whether a string starts with a given character, ignoring leading whitespace.
public static bool StartsWithAfterWhitespace(
this string
text,
character
)
Remove all characters from a string.
public static string Strip(
this string
text,
String to strip.
params char[]
characters
Characters to strip.
)
Stripped string.
Remove all string sequences from a string. This method is recursive, because removing some pattern may yield occurrences of another.
public static string Strip(
this string
text,
String to strip.
params string[]
sequences
String sequences to strip.
)
Stripped string.
Remove all string sequences from a string. This method is recursive, because removing some pattern may yield occurrences of another.
public static string Strip(
this string
text,
String to strip.
comparison,
Comparison to use.
params string[]
sequences
String sequences to strip.
)
Stripped string.

StripAllExcept

#Example
Remove all characters except the ones specified.
public static string StripAllExcept(
this string
text,
Text to modify.
params char[]
characters
Characters to retain.
)
Modified string.

StripAllExcept

#Example
Remove all characters except the ones specified.
public static string StripAllExcept(
this string
text,
Text to modify.
allowedSet
String containing all allowed characters.
)
Modified string.

Substring

#Example
Get a substring using indices.
public static string Substring(
this string
text,
String to operate on.
index0,
First index of sub-string.
index1,
Last index of sub-string.
inclusive
If true, the characters at index0 and index are included.
)
Sub string from i0 to i1.

Substring

#Example
Return the portion of the string *after* the first occurence of the indicated marker.
public static string Substring(
this string
text,
String to cut.
marker,
Marker character,
length
Length of string to return.
)

Substring

#Example
Return the portion of the string *after* the first occurence of the indicated marker.
public static string Substring(
this string
text,
String to cut.
marker,
Marker character,
startIndex,
Character index to start search for marker.
length
Length of string after the marker.
)

Substring

#Example
Return the portion of the string *after* the first occurence of the indicated marker.
public static string Substring(
this string
text,
String to cut.
marker
Marker character,
)

SubstringByGraphemes

#Example
Gets the substring covering countGraphemes clusters starting at the given grapheme-cluster index.
public static string SubstringByGraphemes(
this string
text,
startGrapheme,
countGraphemes
)

SubstringByGraphemes

#Example
Gets the substring starting at the given grapheme-cluster index to the end.
public static string SubstringByGraphemes(
this string
text,
startGrapheme
)

SuperImpose

#Example
Create a new guid by bit-wise superposition of two existing guids.
public static Guid SuperImpose(
this Guid
a,
b
)

ToCompressedString

#Example
Convert a Guid to a short string representation.
public static string ToCompressedString(
this Guid
id
Guid to convert.
)
String representation containing only 17 characters.

ToRegexPattern

#Example
Convert a string with wildcard characters (* and ?) into a regular expression matching string.
public static string ToRegexPattern(
this string
pattern
string to convert.
)
The converted string.

ToString

#Example
Create a string containing only the first few hexadecimal digits of a guid.
public static string ToString(
this Guid
id,
digits
)

ToStringArray

#Example
Convert a string into loose text elements. This is similar to the string.ToCharArray() method except it correctly combines surrogates and diacritics into individual strings. This method merely iterates over the string using System.Globalization.TextElementEnumerator.
public static string[] ToStringArray(
this string
text
String to convert.
)
Array of string elements.
Trim a string to a certain length.
public static string Trim(
this string
text,
String to trim.
length,
Maximum length of the string.
method
Trimming method.
)
Trimmed string.

TrimEnd

#Example
Remove a maximum number of occurences of a set of characters from the end of a string.
public static string TrimEnd(
this string
text,
String to trim.
maximum,
Maximum number of characters to remove.
params char[]
characters
Character set.
)
Trimmed string.

TrimStart

#Example
Remove a maximum number of occurences of a set of characters from the start of a string.
public static string TrimStart(
this string
text,
String to trim.
maximum,
Maximum number of characters to remove.
params char[]
characters
Character set.
)
Trimmed string.

UnquoteDouble

#Example
Remove the leading and trailing double quotes from a string.
public static string UnquoteDouble(
this string
text
)

UnquoteSingle

#Example
Remove the leading and trailing single quotes from a string.
public static string UnquoteSingle(
this string
text
)

WildcardMatch

#Example
Test to see whether this string matches a wildcard pattern. Pattern may contain asterisks (for matching any amount of characters) and question marks (for matching single characters).
public static bool WildcardMatch(
this string
text,
String.
pattern,
String to compare to, may contain wildcards.
comparison
Comparison.
)
True if this string matches the pattern.

WildcardMatch

#Example
Test to see whether this string matches a wildcard pattern. Pattern may contain asterisks (for matching any amount of characters) and question marks (for matching single characters).
public static bool WildcardMatch(
this string
text,
String.
pattern
String to compare to, may contain wildcards.
)
True if this string matches the pattern.
Enumerate over all the words in a string. This iterator treats all whitespace and non-alpha-numeric characters as word separators.
public static IEnumerable<string> Words(
this string
text
String to traverse.
)