C# Класс Westwind.Utilities.StringUtils

String utility class that provides a host of string related operations
Показать файл Открыть проект

Открытые методы

Метод Описание
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: . #

Приватные методы

Метод Описание
Empty ( object text ) : bool
Empty ( string text ) : bool
ParseHexChar ( char c ) : int

Описание методов

Base36Decode() публичный статический Метод

Decodes a base36 encoded string to an integer
public static Base36Decode ( string input ) : long
input string
Результат long

Base36Encode() публичный статический Метод

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
Результат string

BinHexToBinary() публичный статический Метод

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)
Результат byte[]

BinaryToBinHex() публичный статический Метод

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
Результат string

BytesToString() публичный статический Метод

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
Результат string

CountLines() публичный статический Метод

Returns a line count for a string
public static CountLines ( string s ) : int
s string string to count lines for
Результат int

ExtractString() публичный статический Метод

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
Результат string

FromCamelCase() публичный статический Метод

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
Результат string

GetLines() публичный статический Метод

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
Результат string[]

GetProperty() публичный статический Метод

Retrieves a value from an XML-like string
public static GetProperty ( string propertyString, string key ) : string
propertyString string
key string
Результат string

GetUrlEncodedKey() публичный статический Метод

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
Результат string

Inlist() публичный статический Метод

Determines if a string is contained in a list of other strings
public static Inlist ( string s ) : bool
s string
Результат bool

LogString() публичный статический Метод

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
Результат void

NewStringId() публичный статический Метод

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
Результат string

NormalizeIndentation() публичный статический Метод

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
Результат string

ParseDecimal() публичный статический Метод

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
Результат decimal

ParseInt() публичный статический Метод

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
Результат int

ProperCase() публичный статический Метод

Return a string in proper Case format
public static ProperCase ( string Input ) : string
Input string
Результат string

RandomString() публичный статический Метод

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
Результат string

ReplaceString() публичный статический Метод

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
Результат string

ReplaceStringInstance() публичный статический Метод

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
Результат string

Replicate() публичный статический Метод

Replicates a character n number of times and returns a string
public static Replicate ( char character, int charCount ) : string
character char
charCount int
Результат string

Replicate() публичный статический Метод

Replicates an input string n number of times
public static Replicate ( string input, int charCount ) : string
input string
charCount int
Результат string

SetProperty() публичный статический Метод

public static SetProperty ( string propertyString, string key, string value ) : string
propertyString string
key string
value string
Результат string

SetUrlEncodedKey() публичный статический Метод

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
Результат string

StringToBytes() публичный статический Метод

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
Результат byte[]

StringToStream() публичный статический Метод

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
Результат Stream

StripNonNumber() публичный статический Метод

Strips all non digit values from a string and only returns the numeric string.
public static StripNonNumber ( string input ) : string
input string
Результат string

TerminateString() публичный статический Метод

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
Результат string

TextAbstract() публичный статический Метод

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
Результат string

ToCamelCase() публичный статический Метод

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
Результат string

TrimStart() публичный статический Метод

Trims a sub string from a string
public static TrimStart ( string text, string textToTrim, bool caseInsensitive ) : string
text string
textToTrim string
caseInsensitive bool
Результат string

TrimTo() публичный статический Метод

Trims a string to a specific number of max characters
public static TrimTo ( string value, int charCount ) : string
value string
charCount int
Результат string

UrlDecode() публичный статический Метод

UrlDecodes a string without requiring System.Web
public static UrlDecode ( string text ) : string
text string String to decode.
Результат string

UrlEncode() публичный статический Метод

UrlEncodes a string without the requirement for System.Web
public static UrlEncode ( string text ) : string
text string
Результат string

UrlEncodePathSafe() публичный статический Метод

Encodes a few additional characters for use in paths Encodes: . #
public static UrlEncodePathSafe ( string text ) : string
text string
Результат string