C# Class Westwind.Utilities.StringUtils

String utility class that provides a host of string related operations
Mostrar archivo Open project: RickStrahl/WestwindToolkit

Public Methods

Method Description
Base36Decode ( string input ) : long

Decodes a base36 encoded string to an integer

Base36Encode ( long value ) : string

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

BinHexToBinary ( string hex ) : byte[]

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

BinaryToBinHex ( byte data ) : string

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

BytesToString ( byte buffer, Encoding encoding = null ) : string

Converts a byte array to a stringUtils

CountLines ( string s ) : int

Returns a line count for a string

ExtractString ( string source, string beginDelim, string endDelim, bool caseSensitive = false, bool allowMissingEndDelimiter = false, bool returnDelimiters = false ) : string

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

FromCamelCase ( string camelCase ) : string

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.

GetLines ( string s ) : string[]

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

GetProperty ( string propertyString, string key ) : string

Retrieves a value from an XML-like string

GetUrlEncodedKey ( string urlEncoded, string key ) : string

Retrieves a value by key from a UrlEncoded string.

Inlist ( string s ) : bool

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

LogString ( string output, string filename, Encoding encoding = null ) : void

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

NewStringId ( ) : string

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

NormalizeIndentation ( string code ) : string

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.

ParseDecimal ( string input, decimal defaultValue = 0M, IFormatProvider numberFormat = null ) : decimal

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

ParseInt ( string input, int defaultValue, IFormatProvider numberFormat = null ) : int

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

ProperCase ( string Input ) : string

Return a string in proper Case format

RandomString ( int size, bool includeNumbers = false ) : string

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

ReplaceString ( string origString, string findString, string replaceString, bool caseInsensitive ) : string

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

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

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

Replicate ( char character, int charCount ) : string

Replicates a character n number of times and returns a string

Replicate ( string input, int charCount ) : string

Replicates an input string n number of times

SetProperty ( string propertyString, string key, string value ) : string

SetUrlEncodedKey ( string urlEncoded, string key, string value ) : string

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

StringToBytes ( string text, Encoding encoding = null ) : byte[]

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

StringToStream ( string text, Encoding encoding = null ) : Stream

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

StripNonNumber ( string input ) : string

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

TerminateString ( string value, string terminator ) : string

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

TextAbstract ( string text, int length ) : string

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.

ToCamelCase ( string phrase ) : string

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

TrimStart ( string text, string textToTrim, bool caseInsensitive ) : string

Trims a sub string from a string

TrimTo ( string value, int charCount ) : string

Trims a string to a specific number of max characters

UrlDecode ( string text ) : string

UrlDecodes a string without requiring System.Web

UrlEncode ( string text ) : string

UrlEncodes a string without the requirement for System.Web

UrlEncodePathSafe ( string text ) : string

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

Private Methods

Method Description
Empty ( object text ) : bool
Empty ( string text ) : bool
ParseHexChar ( char c ) : int

Method Details

Base36Decode() public static method

Decodes a base36 encoded string to an integer
public static Base36Decode ( string input ) : long
input string
return long

Base36Encode() public static method

Encodes an integer into a string by mapping to alpha and digits (36 chars) chars are embedded as lower case Example: 4zx12ss
public static Base36Encode ( long value ) : string
value long
return string

BinHexToBinary() public static method

Turns a BinHex string that contains raw byte values into a byte array
public static BinHexToBinary ( string hex ) : byte[]
hex string BinHex string (just two byte hex digits strung together)
return byte[]

BinaryToBinHex() public static method

Converts a byte array into a BinHex string. BinHex is two digit hex byte values squished together into a string.
public static BinaryToBinHex ( byte data ) : string
data byte Raw data to send
return string

BytesToString() public static method

Converts a byte array to a stringUtils
public static BytesToString ( byte buffer, Encoding encoding = null ) : string
buffer byte raw string byte data
encoding System.Text.Encoding Character encoding to use. Defaults to Unicode
return string

CountLines() public static method

Returns a line count for a string
public static CountLines ( string s ) : int
s string string to count lines for
return int

ExtractString() public static method

Extracts a string from between a pair of delimiters. Only the first instance is found.
public static ExtractString ( string source, string beginDelim, string endDelim, bool caseSensitive = false, bool allowMissingEndDelimiter = false, bool returnDelimiters = false ) : string
source string Input String to work on
beginDelim string
endDelim string ending delimiter
caseSensitive bool
allowMissingEndDelimiter bool
returnDelimiters bool
return string

FromCamelCase() public static method

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 FromCamelCase ( string camelCase ) : string
camelCase string
return string

GetLines() public static method

Parses a string into an array of lines broken by \r\n or \n
public static GetLines ( string s ) : string[]
s string String to check for lines
return string[]

GetProperty() public static method

Retrieves a value from an XML-like string
public static GetProperty ( string propertyString, string key ) : string
propertyString string
key string
return string

GetUrlEncodedKey() public static method

Retrieves a value by key from a UrlEncoded string.
public static GetUrlEncodedKey ( string urlEncoded, string key ) : string
urlEncoded string UrlEncoded String
key string Key to retrieve value for
return string

Inlist() public static method

Determines if a string is contained in a list of other strings
public static Inlist ( string s ) : bool
s string
return bool

LogString() public static method

Simple Logging method that allows quickly writing a string to a file
public static LogString ( string output, string filename, Encoding encoding = null ) : void
output string
filename string
encoding System.Text.Encoding if not specified used UTF-8
return void

NewStringId() public static method

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 NewStringId ( ) : string
return string

NormalizeIndentation() public static method

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 NormalizeIndentation ( string code ) : string
code string
return string

ParseDecimal() public static method

Parses an string into an decimal. If the value can't be parsed a default value is returned instead
public static ParseDecimal ( string input, decimal defaultValue = 0M, IFormatProvider numberFormat = null ) : decimal
input string
defaultValue decimal
numberFormat IFormatProvider
return decimal

ParseInt() public static method

Parses an string into an integer. If the value can't be parsed a default value is returned instead
public static ParseInt ( string input, int defaultValue, IFormatProvider numberFormat = null ) : int
input string Input numeric string to be parsed
defaultValue int Optional default value if parsing fails
numberFormat IFormatProvider
return int

ProperCase() public static method

Return a string in proper Case format
public static ProperCase ( string Input ) : string
Input string
return string

RandomString() public static method

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 RandomString ( int size, bool includeNumbers = false ) : string
size int The number of characters of the string to generate
includeNumbers bool
return string

ReplaceString() public static method

Replaces a substring within a string with another substring with optional case sensitivity turned off.
public static ReplaceString ( string origString, string findString, string replaceString, bool caseInsensitive ) : string
origString string String to do replacements on
findString string The string to find
replaceString string The string to replace found string wiht
caseInsensitive bool If true case insensitive search is performed
return string

ReplaceStringInstance() public static method

String replace function that supports replacing a specific instance with case insensitivity
public static ReplaceStringInstance ( string origString, string findString, string replaceWith, int instance, bool caseInsensitive ) : string
origString string Original input string
findString string The string that is to be replaced
replaceWith string The replacement string
instance int Instance of the FindString that is to be found. if Instance = -1 all are replaced
caseInsensitive bool Case insensitivity flag
return string

Replicate() public static method

Replicates a character n number of times and returns a string
public static Replicate ( char character, int charCount ) : string
character char
charCount int
return string

Replicate() public static method

Replicates an input string n number of times
public static Replicate ( string input, int charCount ) : string
input string
charCount int
return string

SetProperty() public static method

public static SetProperty ( string propertyString, string key, string value ) : string
propertyString string
key string
value string
return string

SetUrlEncodedKey() public static method

Allows setting of a value in a UrlEncoded string. If the key doesn't exist a new one is set, if it exists it's replaced with the new value.
public static SetUrlEncodedKey ( string urlEncoded, string key, string value ) : string
urlEncoded string A UrlEncoded string of key value pairs
key string
value string
return string

StringToBytes() public static method

Converts a string into bytes for storage in any byte[] types buffer or stream format (like MemoryStream).
public static StringToBytes ( string text, Encoding encoding = null ) : byte[]
text string
encoding System.Text.Encoding The character encoding to use. Defaults to Unicode
return byte[]

StringToStream() public static method

Creates a Stream from a string. Internally creates a memory stream and returns that.
public static StringToStream ( string text, Encoding encoding = null ) : Stream
text string
encoding System.Text.Encoding
return Stream

StripNonNumber() public static method

Strips all non digit values from a string and only returns the numeric string.
public static StripNonNumber ( string input ) : string
input string
return string

TerminateString() public static method

Terminates a string with the given end string/character, but only if the value specified doesn't already exist and the string is not empty.
public static TerminateString ( string value, string terminator ) : string
value string String to terminate
terminator string String to terminate the value string with
return string

TextAbstract() public static method

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 TextAbstract ( string text, int length ) : string
text string Text to abstract
length int Number of characters to abstract to
return string

ToCamelCase() public static method

Takes a phrase and turns it into CamelCase text. White Space, punctuation and separators are stripped
public static ToCamelCase ( string phrase ) : string
phrase string Text to convert to CamelCase
return string

TrimStart() public static method

Trims a sub string from a string
public static TrimStart ( string text, string textToTrim, bool caseInsensitive ) : string
text string
textToTrim string
caseInsensitive bool
return string

TrimTo() public static method

Trims a string to a specific number of max characters
public static TrimTo ( string value, int charCount ) : string
value string
charCount int
return string

UrlDecode() public static method

UrlDecodes a string without requiring System.Web
public static UrlDecode ( string text ) : string
text string String to decode.
return string

UrlEncode() public static method

UrlEncodes a string without the requirement for System.Web
public static UrlEncode ( string text ) : string
text string
return string

UrlEncodePathSafe() public static method

Encodes a few additional characters for use in paths Encodes: . #
public static UrlEncodePathSafe ( string text ) : string
text string
return string