Table of Contents

Class XString

Extension methods for String.

Inheritance
System.Object
XString
Namespace: Galaxon.Core.Strings
Assembly: Galaxon.Core.dll
Syntax
public static class XString : object

Fields

SmallCapsChars

Map from lower-case letters to their Unicode small caps equivalents.

Declaration
public static readonly Dictionary<char, string> SmallCapsChars
Field Value
Type Description
Dictionary<System.Char, System.String>
| Improve this Doc View Source

Methods

EqualsIgnoreCase(String, Nullable<String>)

See if 2 strings are equal, ignoring case.

Declaration
public static bool EqualsIgnoreCase(this string str1, string? str2)
Parameters
Type Name Description
System.String str1
System.Nullable<System.String> str2
Returns
Type Description
System.Boolean
| Improve this Doc View Source

GroupDigits(String, Char, Byte)

Given a string of digits, format in groups using the specified group separator and group size.

This method is designed for formatting numbers but it could be used for other purposes, since the method doesn't check if the characters are actually digits. It just assumes they are. Apart from saving time, it allows the method to be used for hexadecimal or other bases.

Grouping starts from the right. Here's how you would format an integer: "12345678".GroupDigits(',', 3) => "12,345,678"

You can chain methods if you need to, e.g. "11111000000001010101".GroupDigits('', 8) => "1111_10000000_01010101" "11111000000001010101".ZeroPad(24).GroupDigits('', 8) => "00001111_10000000_01010101" 123456789.ToHex().ZeroPad(8).GroupDigits(' ') => "075b cd15"

Declaration
public static string GroupDigits(this string str, char separator = '_', byte size = null)
Parameters
Type Name Description
System.String str

The string, nominally of digits, but can be whatever.

System.Char separator

The group separator character.

System.Byte size

The group size.

Returns
Type Description
System.String

The formatted string.

| Improve this Doc View Source

IsAscii(String)

Check if a string contains only ASCII characters.

Declaration
public static bool IsAscii(this string str)
Parameters
Type Name Description
System.String str

The string to check.

Returns
Type Description
System.Boolean
| Improve this Doc View Source

IsPalindrome(String)

Check if a string is a palindrome.

Declaration
public static bool IsPalindrome(this string str)
Parameters
Type Name Description
System.String str
Returns
Type Description
System.Boolean
| Improve this Doc View Source

MakeSlug(String)

Convert a Unicode string (for example, a page title) to a user-, browser-, and search engine-friendly URL slug containing only lower-case alphanumeric ASCII characters and hyphens. This method does not remove short words like "a", "the", "of", etc., like some algorithms do. I don't perceive this as necessary - please let me know if you disagree.

Declaration
public static string MakeSlug(this string str)
Parameters
Type Name Description
System.String str

The string to process.

Returns
Type Description
System.String

The ASCII slug.

| Improve this Doc View Source

Repeat(String, Int32)

Construct a new string by repeating a string multiple times.

Declaration
public static string Repeat(string s, int n)
Parameters
Type Name Description
System.String s
System.Int32 n
Returns
Type Description
System.String
| Improve this Doc View Source

ReplaceChars(String, Dictionary<Char, String>, Boolean)

Replace characters in a string with other characters by using a character map. Example use cases:

  • making a string upper- or lower-case
  • converting lowercase characters to small caps
  • making a string superscript or subscript
  • transliteration/removal of diacritics
Declaration
public static string ReplaceChars(this string str, Dictionary<char, string> charMap, bool keepCharsNotInMap = true)
Parameters
Type Name Description
System.String str

The original string.

Dictionary<System.Char, System.String> charMap

The character map.

System.Boolean keepCharsNotInMap

If a character is encountered that is not in the character map, either keep it (true) or skip it (false).

Returns
Type Description
System.String

The transformed string.

| Improve this Doc View Source

Reverse(String)

Reverse a string. e.g. "You are awesome." becomes ".emosewa era uoY".

Declaration
public static string Reverse(this string str)
Parameters
Type Name Description
System.String str
Returns
Type Description
System.String
| Improve this Doc View Source

StripBrackets(String, Boolean, Boolean, Boolean, Boolean)

Remove brackets (and whatever's between them) from a string.

Declaration
public static string StripBrackets(this string str, bool round = true, bool square = true, bool curly = true, bool angle = true)
Parameters
Type Name Description
System.String str

The string to process.

System.Boolean round

If round brackets should be removed.

System.Boolean square

If square brackets should be removed

System.Boolean curly

If curly brackets should be removed

System.Boolean angle

If angle brackets should be removed

Returns
Type Description
System.String

The string with brackets removed.

| Improve this Doc View Source

StripTags(String)

Strip HTML tags from a string.

Declaration
public static string StripTags(this string str)
Parameters
Type Name Description
System.String str

The string to process.

Returns
Type Description
System.String

The string with HTML tags removed.

| Improve this Doc View Source

StripWhitespace(String)

Remove whitespace from a string.

Declaration
public static string StripWhitespace(this string str)
Parameters
Type Name Description
System.String str

The string to process.

Returns
Type Description
System.String

The string with whitespace characters removed.

| Improve this Doc View Source

ToSmallCaps(String)

Convert all lower-case letters in a string to their Unicode small caps variant. Only works for English letters, so, if necessary (e.g. if the string is in a different language), you may wish to call AnyAscii.Transliterate() on the string first.

Declaration
public static string ToSmallCaps(this string str)
Parameters
Type Name Description
System.String str
Returns
Type Description
System.String
| Improve this Doc View Source

ToSubscript(String)

Render a string with valid integer characters (i.e. minus sign or digits) converted to their Unicode subscript versions.

Declaration
public static string ToSubscript(this string str)
Parameters
Type Name Description
System.String str

The string.

Returns
Type Description
System.String

The string of subscript characters.

| Improve this Doc View Source

ToSuperscript(String)

Render a string with valid integer characters (i.e. minus sign or digits) converted to their Unicode superscript versions.

Declaration
public static string ToSuperscript(this string str)
Parameters
Type Name Description
System.String str

The string.

Returns
Type Description
System.String

The string of superscript characters.

| Improve this Doc View Source

ZeroPad(String, Int32)

Pad a string on the left with 0s to make it up to a certain width.

Declaration
public static string ZeroPad(this string str, int width)
Parameters
Type Name Description
System.String str

The string.

System.Int32 width

The minimum number of characters in the the result.

Returns
Type Description
System.String

The zero-padded string.

| Improve this Doc View Source
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX | Galaxy logo by Freepik