StringExtensionMethods

public static class StringExtensionMethods
Extension methods for System.String

Methods

public static string[] Break(this string text, Font font, int fragments, char[] leadingBreaks = null, char[] trailingBreaks = null)
Try and break a string into a fixed number of fragments at custom characters.
text (string)String to break.
font (Font)Font to use for measuring.
fragments (int)Target number of fragments.
leadingBreaks (char[] = null)Spitting characters. Splits will be made following these characters.
trailingBreaks (char[] = 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
public static string CollapseWhiteSpace(this string text, char symbol = '\0')
Collapse all whitespace runs with a single symbol.
text (string)String to modify.
symbol (char = '')Symbol to insert for a white-space run. Leave at 'null' to not insert any symbol.
Collapsed string.

CommonOverlap

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

CompressedStringToGuid

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

Consume

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

Consume

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

ConsumeUntil

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

Contains

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

ContainsIsolated

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

ContainsIsolated

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

DecodeEscapedCharacters

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

DecodeWhitespace

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

DropSuperScript

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

EncodeWhitespace

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

EndsWithBeforeWhitespace

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

EqualsAny

#Example
public static bool EqualsAny(this string text, StringComparison comparison, params string[] matches)
Check whether a string is equal to any one of a set of strings.
text (string)String to compare.
comparison (StringComparison)Comparison type.
matches (params string[])Set of possible matches.
True if string is equal to at least one of the matches.
public static int[] Find(this string text, char character, Mask mask, int firstChar, int lastChar)
Finds all the occurrences of a character within a substring.
text (string)String.
character (char)Character to search for.
mask (Prose.Mask)String mask.
firstChar (int)Index of first character to take into account.
lastChar (int)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.
public static int[] Find(this string text, char character, Mask mask)
Finds all the occurrences of a character within this string.
text (string)String.
character (char)Character to search for.
mask (Prose.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
public static int[] FindAll(this string text, Mask filter, params char[] characters)
Find all occurrences of a specific set of characters.
text (string)String to search.
filter (Prose.Mask)Filtering mask.
characters (params char[])Characters to search for.
Array of character indices.

FindAll

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

FindIsolated

#Example
public static int FindIsolated(this string text, string pattern, int startIndex, StringComparison comparisonType)
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.
text (string)String to search.
pattern (string)String to search for.
startIndex (int)First index to include in the search.
comparisonType (StringComparison)Comparison to use.
Index at which isolated substring occurs or -1 if no isolated string could be found.

FindIsolated

#Example
public static int FindIsolated(this string text, string pattern, StringComparison comparisonType)
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.
text (string)String to search.
pattern (string)String to search for.
comparisonType (StringComparison)Comparison to use.
Index at which isolated substring occurs or -1 if no isolated string could be found.

FindLineStart

#Example
public static int FindLineStart(this string text, int caret)
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.
text (string)
caret (int)

FindMatchingBrace

#Example
public static int FindMatchingBrace(this string text, int start, char open = '(', char close = ')')
Find a matching brace pair.
text (string)String to search.
start (int)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 (char = '(')Open brace symbol.
close (char = ')')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
public static int FindNext(this string text, char character, Mask mask, int startChar)
Find the next occurance of a specific character in this string.
text (string)String.
character (char)Character to search for.
mask (Prose.Mask)Searching options.
startChar (int)
The index of the first character that matches the search char, or -1 if no such char could be found.

FindSymbolRegion

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

FindWhiteRegion

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

FindWordRegion

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

GraphemeCount

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

GraphemeToCodeUnit

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

IsHexDigit

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

IsSingleLineString

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

NextTransition

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

OffsetAndAlign

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

OffsetAndAlign

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

OffsetAndAlign

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

PadToWidest

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

PrevTransition

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

RemoveAll

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

RemoveAll

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

RemoveAll

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

RemoveSurroundingBraces

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

RemoveWhiteSpace

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

RemoveWhiteSpace

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

Replace

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

Replace

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

Replace

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

Replace

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

Replace

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

ReplaceRepeatedly

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

SimplifyNewLines

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

SplitAtChar

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

SplitAtIndex

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

SplitLines

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

StartsWithAfterWhitespace

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

StripAllExcept

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

StripAllExcept

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

Substring

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

Substring

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

Substring

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

Substring

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

SubstringByGraphemes

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

SubstringByGraphemes

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

SuperImpose

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

ToCompressedString

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

ToRegexPattern

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

ToString

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

ToStringArray

#Example
public static string[] ToStringArray(this string text)
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.
text (string)String to convert.
Array of string elements.
public static string Trim(this string text, int length, TrimmingMethod method)
Trim a string to a certain length.
text (string)String to trim.
length (int)Maximum length of the string.
method (TrimmingMethod)Trimming method.
Trimmed string.

TrimEnd

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

TrimStart

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

UnquoteDouble

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

UnquoteSingle

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

WildcardMatch

#Example
public static bool WildcardMatch(this string text, string pattern, StringComparison comparison)
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).
text (string)String.
pattern (string)String to compare to, may contain wildcards.
comparison (StringComparison)Comparison.
True if this string matches the pattern.

WildcardMatch

#Example
public static bool WildcardMatch(this string text, string pattern)
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).
text (string)String.
pattern (string)String to compare to, may contain wildcards.
True if this string matches the pattern.
public static IEnumerable<string> Words(this string text)
Enumerate over all the words in a string. This iterator treats all whitespace and non-alpha-numeric characters as word separators.
text (string)String to traverse.