Class StringUtils

String utility class that provides a host of string related operations

public static class StringUtils : object

Class Members



Decodes a base36 encoded string to an integer

public static long Base36Decode(string input)


Encodes an integer into a string by mapping to alpha and digits (36 chars) chars are embedded as lower case

Example: 4zx12ss

public static string Base36Encode(long value)


Converts a byte array into a BinHex string. BinHex is two digit hex byte values squished together into a string.

public static string BinaryToBinHex(Byte[] data)


Turns a BinHex string that contains raw byte values into a byte array

public static Byte[] BinHexToBinary(string hex)


Converts a byte array to a stringUtils

public static string BytesToString(Byte[] buffer,     Encoding encoding)


String.Contains() extension method that allows to specify case

public static bool Contains(string text,     string searchFor,     StringComparison stringComparison)


Returns a line count for a string

public static int CountLines(string s)


Detokenizes a string tokenized with TokenizeString. Requires the collection created by detokenization

public static string DetokenizeString(string text,     List tokens,     string replaceDelimiter)


Extracts a string from between a pair of delimiters. Only the first instance is found.

public static string ExtractString(string source,     string beginDelim,     string endDelim,     bool caseSensitive,     bool allowMissingEndDelimiter,     bool returnDelimiters)


Tries to create a phrase string from CamelCase text. Will place spaces before capitalized letters.

Note that this method may not work for round tripping ToCamelCase calls, since ToCamelCase strips more characters than just spaces.

public static string FromCamelCase(string camelCase)


Parses a string into an array of lines broken by \r\n or \n

public static String[] GetLines(string s,     int maxLines)


Retrieves a text from an XML-like string

public static string GetProperty(string propertyString,     string key)


Retrieves a text by key from a UrlEncoded string.

public static string GetUrlEncodedKey(string urlEncoded,     string key)


Finds the nth index of string in a string

public static int IndexOfNth(string source,     string matchString,     int stringInstance,     StringComparison stringComparison)

public static int IndexOfNth(string source,     char matchChar,     int charInstance)


Determines if a string is contained in a list of other strings

public static bool Inlist(string s,     String[] list)


Finds the nth index of strting in a string

public static int LastIndexOfNth(string source,     string matchString,     int charInstance,     StringComparison stringComparison)

public static int LastIndexOfNth(string source,     char matchChar,     int charInstance)


Simple Logging method that allows quickly writing a string to a file

public static void LogString(string output,     string filename,     Encoding encoding)


Creates short string id based on a GUID hashcode. Not guaranteed to be unique across machines, but unlikely to duplicate in medium volume situations.

public static string NewStringId()


Strips any common white space from all lines of text that have the same common white space text. Effectively removes common code indentation from code blocks for example so you can get a left aligned code snippet.

public static string NormalizeIndentation(string code)


Normalizes linefeeds to the appropriate

public static string NormalizeLineFeeds(string text,     LineFeedTypes type)


Parses an string into an decimal. If the text can't be parsed a default text is returned instead

public static decimal ParseDecimal(string input,     decimal defaultValue,     IFormatProvider numberFormat)


Parses an string into an integer. If the text can't be parsed a default text is returned instead

public static int ParseInt(string input,     int defaultValue,     IFormatProvider numberFormat)


Return a string in proper Case format

public static string ProperCase(string Input)


Creates a new random string of upper, lower case letters and digits. Very useful for generating random data for storage in test data.

public static string RandomString(int size,     bool includeNumbers)


Replaces a substring within a string with another substring with optional case sensitivity turned off.

public static string ReplaceString(string origString,     string findString,     string replaceString,     bool caseInsensitive)


String replace function that supports replacing a specific instance with case insensitivity

public static string ReplaceStringInstance(string origString,     string findString,     string replaceWith,     int instance,     bool caseInsensitive)


Replicates an input string n number of times

public static string Replicate(string input,     int charCount)

public static string Replicate(char character,     int charCount)


Allows setting of a text in a UrlEncoded string. If the key doesn't exist a new one is set, if it exists it's replaced with the new text.

public static string SetUrlEncodedKey(string urlEncoded,     string key,     string value)


Converts a string into bytes for storage in any byte[] types buffer or stream format (like MemoryStream).

public static Byte[] StringToBytes(string text,     Encoding encoding)


Creates a Stream from a string. Internally creates a memory stream and returns that.

public static Stream StringToStream(string text,     Encoding encoding)


Strips all non digit values from a string and only returns the numeric string.

public static string StripNonNumber(string input)


Terminates a string with the given end string/character, but only if the text specified doesn't already exist and the string is not empty.

public static string TerminateString(string value,     string terminator)


Returns an abstract of the provided text by returning up to Length characters of a text string. If the text is truncated a ... is appended.

public static string TextAbstract(string text,     int length)


Takes a phrase and turns it into CamelCase text. White Space, punctuation and separators are stripped

public static string ToCamelCase(string phrase)


Tokenizes a string based on a start and end string. Replaces the values with a token text (#@#1#@# for example).

You can use Detokenize to get the original values back

public static List TokenizeString(ref string text,     string start,     string end,     string replaceDelimiter)


Trims a sub string from a string.

public static string TrimStart(string text,     string textToTrim,     bool caseInsensitive)


Truncate a string to maximum length.

public static string Truncate(string text,     int maxLength)


UrlDecodes a string without requiring System.Web

public static string UrlDecode(string text)


UrlEncodes a string without the requirement for System.Web

public static string UrlEncode(string text)


Encodes a few additional characters for use in paths Encodes: . #

public static string UrlEncodePathSafe(string text)


Namespace: Westwind.Utilities
Assembly: westwind.utilities.dll

© West Wind Technologies, 2019 • Updated: 06/23/19
Comment or report problem with topic