StringHelper
Provides helper methods for string operations.
属性
public static Encoding WindowsCyrillic { get; }
value = StringHelper.WindowsCyrillic
Gets the Windows Cyrillic encoding (code page 1251).
方法
public static SmartFormatter AddDictionarySourceEx(SmartFormatter formatter)
result = StringHelper.AddDictionarySourceEx(formatter)
Adds DictionarySourceEx to the formatter. Handles IDictionary with any key type (enum, int, etc.) and returns null for missing keys instead of throwing.
public static string ASCII(ReadOnlySpan<byte> v)
result = StringHelper.ASCII(v)
Gets the string from a span of bytes using ASCII encoding.
- v
- The span of bytes.
返回值: The ASCII decoded string.
public static string ASCII(byte[] v)
result = StringHelper.ASCII(v)
Gets the string from a byte array using ASCII encoding.
- v
- The byte array.
返回值: The ASCII decoded string.
public static string ASCII(byte[] v, int index, int count)
result = StringHelper.ASCII(v, index, count)
Gets the string from a portion of the byte array using ASCII encoding.
- v
- The byte array.
- index
- The starting index.
- count
- The number of bytes to decode.
返回值: The decoded string.
public static string ASCII(byte[] v, uint count, int index)
result = StringHelper.ASCII(v, count, index)
Gets the string from a byte array using ASCII encoding with a specified count.
- v
- The byte array.
- count
- The number of bytes to decode.
- index
- The starting index.
返回值: The decoded string.
public static int ASCII(ReadOnlySpan<char> v, Span<byte> destination)
result = StringHelper.ASCII(v, destination)
Encodes a span of characters into a span of bytes using ASCII encoding.
- v
- The span of characters to encode.
- destination
- The destination span to write the encoded bytes.
返回值: The number of bytes written to the destination.
public static byte[] ASCII(string v)
result = StringHelper.ASCII(v)
Gets the ASCII encoded bytes for the string.
- v
- The input string.
返回值: The ASCII byte array.
public static byte[] Base64(string value)
result = StringHelper.Base64(value)
Converts a base64 encoded string to a byte array.
- value
- The base64 encoded string.
返回值: The byte array representation.
public static string Base64(byte[] value)
result = StringHelper.Base64(value)
Converts a byte array to a base64 encoded string.
- value
- The byte array.
返回值: The base64 encoded string.
public static bool CheckBrackets(string str, string sStart, string sEnd)
result = StringHelper.CheckBrackets(str, sStart, sEnd)
Checks whether the string starts with the specified start string and ends with the specified end string.
- str
- The input string.
- sStart
- The starting string.
- sEnd
- The ending string.
返回值: true if the string is enclosed by the specified brackets; otherwise, false.
public static bool ComparePaths(string path1, string path2)
result = StringHelper.ComparePaths(path1, path2)
Compares two file paths for equality after normalizing them.
- path1
- The first file path.
- path2
- The second file path.
返回值: true if the paths are equal; otherwise, false.
public static bool ContainsIgnoreCase(string str1, string str2)
result = StringHelper.ContainsIgnoreCase(str1, str2)
Determines whether the first string contains the second string, ignoring case.
- str1
- The string to search.
- str2
- The string to locate.
返回值: True if str1 contains str2 when ignoring case; otherwise, false.
public static SmartFormatter CreateSmartFormatterEx()
result = StringHelper.CreateSmartFormatterEx()
Creates a pre-configured SmartFormatter with DictionarySourceEx and DefaultFormatter.Name = "default".
public static string Cyrillic(byte[] v)
result = StringHelper.Cyrillic(v)
Gets the string from a byte array using Windows Cyrillic encoding.
- v
- The byte array.
返回值: The decoded string.
public static string Cyrillic(byte[] v, int index, int count)
result = StringHelper.Cyrillic(v, index, count)
Gets the string from a portion of the byte array using Windows Cyrillic encoding.
- v
- The byte array.
- index
- The starting index.
- count
- The number of bytes to decode.
返回值: The decoded string.
public static string Cyrillic(byte[] v, uint count, int index)
result = StringHelper.Cyrillic(v, count, index)
Gets the string from a byte array using Windows Cyrillic encoding with a specified count.
- v
- The byte array.
- count
- The number of bytes to decode.
- index
- The starting index.
返回值: The decoded string.
public static byte[] Cyrillic(string v)
result = StringHelper.Cyrillic(v)
Gets the Windows Cyrillic encoded bytes for the string.
- v
- The input string.
返回值: The Windows Cyrillic byte array.
public static string Cyrillic(ReadOnlySpan<byte> v)
result = StringHelper.Cyrillic(v)
Gets the string from a span of bytes using Windows Cyrillic encoding.
- v
- The span of bytes.
返回值: The decoded string.
public static int Cyrillic(ReadOnlySpan<char> v, Span<byte> destination)
result = StringHelper.Cyrillic(v, destination)
Encodes a span of characters into a span of bytes using Windows Cyrillic encoding.
- v
- The span of characters to encode.
- destination
- The destination span to write the encoded bytes.
返回值: The number of bytes written to the destination.
public static string DataEscape(string url)
result = StringHelper.DataEscape(url)
Escapes URL data.
- url
- The URL data to escape.
返回值: An escaped URL data string.
public static string DataUnEscape(string url)
result = StringHelper.DataUnEscape(url)
Unescapes URL data.
- url
- The URL data to unescape.
返回值: An unescaped URL data string.
public static string Default(byte[] v, int index, int count)
result = StringHelper.Default(v, index, count)
Gets the string from a portion of the byte array using the default encoding.
- v
- The byte array.
- index
- The starting index.
- count
- The number of bytes to decode.
返回值: The decoded string.
public static byte[] Default(string v)
result = StringHelper.Default(v)
Gets the default encoding bytes for the string.
- v
- The input string.
返回值: The byte array in default encoding.
public static string Default(ReadOnlySpan<byte> v)
result = StringHelper.Default(v)
Gets the string from a span of bytes using the default encoding.
- v
- The span of bytes.
返回值: The decoded string.
public static int Default(ReadOnlySpan<char> v, Span<byte> destination)
result = StringHelper.Default(v, destination)
Encodes a span of characters into a span of bytes using the default encoding.
- v
- The span of characters to encode.
- destination
- The destination span to write the encoded bytes.
返回值: The number of bytes written to the destination.
public static string Default(byte[] v)
result = StringHelper.Default(v)
Gets the string from a byte array using the default encoding.
- v
- The byte array.
返回值: The string decoded from the byte array.
public static string Default(byte[] v, uint count, int index)
result = StringHelper.Default(v, count, index)
Gets the string from a byte array using the default encoding with a specified count.
- v
- The byte array.
- count
- The number of bytes to decode.
- index
- The starting index.
返回值: The decoded string.
public static string Digest(byte[] digest)
result = StringHelper.Digest(digest)
Converts the byte array digest to its hexadecimal string representation.
- digest
- The byte array digest.
返回值: The hexadecimal string.
public static string Digest(byte[] digest, int? length, int index)
result = StringHelper.Digest(digest, length, index)
Converts a portion of the byte array digest to its hexadecimal string representation.
- digest
- The byte array digest.
- length
- The number of bytes to process.
- index
- The starting index in the array.
返回值: The hexadecimal string.
public static IEnumerable<string> Duplicates(IEnumerable<string> items)
result = StringHelper.Duplicates(items)
Returns the duplicate strings from the provided collection, ignoring case.
- items
- The collection of strings.
返回值: An enumerable collection of duplicate strings.
public static IEnumerable<string> Duplicates(IEnumerable<string> items, IEqualityComparer<string> comparer)
result = StringHelper.Duplicates(items, comparer)
Returns the duplicate strings from the provided collection using the specified comparer.
- items
- The collection of strings.
- comparer
- The string comparer to use for determining duplicates.
返回值: An enumerable collection of duplicate strings.
private static Regex EmailValidationRegex()
result = StringHelper.EmailValidationRegex()
public static bool EndsWithIgnoreCase(string str1, string str2)
result = StringHelper.EndsWithIgnoreCase(str1, str2)
Determines whether the string ends with the specified substring, ignoring case.
- str1
- The string to test.
- str2
- The substring to compare.
返回值: True if str1 ends with str2 ignoring case; otherwise, false.
public static bool EqualsIgnoreCase(string str1, string str2)
result = StringHelper.EqualsIgnoreCase(str1, str2)
Determines whether two strings are equal, ignoring case.
- str1
- The first string to compare.
- str2
- The second string to compare.
返回值: True if the strings are equal ignoring case; otherwise, false.
public static int FastIndexOf(string source, string pattern)
result = StringHelper.FastIndexOf(source, pattern)
Searches for the specified pattern in the source string and returns the zero-based index of its first occurrence.
- source
- The source string to search.
- pattern
- The pattern to locate.
返回值: The index of the first occurrence; otherwise, -1 if not found.
public static string GetAndClear(StringBuilder builder)
result = StringHelper.GetAndClear(builder)
Retrieves the content of the StringBuilder and then clears it.
- builder
- The StringBuilder.
返回值: The content of the StringBuilder before it was cleared.
public static int GetDeterministicHashCode(string value)
result = StringHelper.GetDeterministicHashCode(value)
Calculates a deterministic hash code for the specified string.
- value
- The string to hash.
返回值: An integer hash code computed deterministically.
public static string GetLangCode(string cultureName)
result = StringHelper.GetLangCode(cultureName)
Gets the two-letter ISO language code from the provided culture name.
- cultureName
- The culture name.
返回值: The two-letter ISO language code.
public static string Hex(byte[] v)
result = StringHelper.Hex(v)
Gets the string from a byte array using hexadecimal encoding.
- v
- The byte array.
返回值: The hexadecimal decoded string.
public static string Hex(byte[] v, int index, int count)
result = StringHelper.Hex(v, index, count)
Gets the string from a portion of the byte array using hexadecimal encoding.
- v
- The byte array.
- index
- The starting index.
- count
- The number of bytes to decode.
返回值: The decoded hexadecimal string.
public static string Hex(byte[] v, uint count, int index)
result = StringHelper.Hex(v, count, index)
Gets the string from a byte array using hexadecimal encoding with a specified count.
- v
- The byte array.
- count
- The number of bytes to decode.
- index
- The starting index.
返回值: The decoded hexadecimal string.
public static byte[] Hex(string v)
result = StringHelper.Hex(v)
Gets the hexadecimal encoded bytes for the string.
- v
- The input string.
返回值: The hexadecimal byte array.
public static int IndexOfIgnoreCase(string str1, string str2, int index)
result = StringHelper.IndexOfIgnoreCase(str1, str2, index)
Returns the zero-based index of the first occurrence of the specified substring, ignoring case.
- str1
- The string to search.
- str2
- The substring to locate.
- index
- The starting index for the search. Defaults to -1 for beginning.
返回值: The index of the first occurrence, or -1 if not found.
public static string Intern(string str)
result = StringHelper.Intern(str)
Interns the specified string.
- str
- The string to intern.
返回值: The interned string.
public static bool IsDigit(char c)
result = StringHelper.IsDigit(c)
Determines whether the specified character is a digit.
- c
- The character to test.
返回值: True if the character is a digit; otherwise, false.
public static string IsEmpty(string str, string defaultValue)
result = StringHelper.IsEmpty(str, defaultValue)
Returns default value if the string is null or empty; otherwise, returns the original string.
- str
- The string to check.
- defaultValue
- The value to return if string is null or empty.
返回值: Default value or original string.
public static bool IsEmpty(string str)
result = StringHelper.IsEmpty(str)
Checks if the string is null or empty.
- str
- The string to check.
返回值: True if the string is null or empty; otherwise, false.
public static bool IsEmpty(SecureString secureString)
result = StringHelper.IsEmpty(secureString)
Determines whether the specified secure string is empty.
- secureString
- The secure string to check.
返回值: true if the secure string is null or empty; otherwise, false.
public static bool IsEmpty(StringBuilder builder)
result = StringHelper.IsEmpty(builder)
Determines whether the StringBuilder is empty.
- builder
- The StringBuilder to evaluate.
返回值: True if the StringBuilder is empty; otherwise, false.
public static bool IsEmptyOrWhiteSpace(string str)
result = StringHelper.IsEmptyOrWhiteSpace(str)
Checks if the string is null, empty, or consists only of white-space characters.
- str
- The string to check.
返回值: True if the string is null, empty, or whitespace; otherwise, false.
public static string IsEmptyOrWhiteSpace(string str, string defaultValue)
result = StringHelper.IsEmptyOrWhiteSpace(str, defaultValue)
Returns default value if the string is null, empty, or whitespace; otherwise, returns the original string.
- str
- The string to check.
- defaultValue
- The value to return if string is null, empty, or whitespace.
返回值: Default value or original string.
public static bool IsEqualTo(SecureString value1, SecureString value2)
result = StringHelper.IsEqualTo(value1, value2)
Determines whether two secure strings are equal.
- value1
- The first secure string.
- value2
- The second secure string.
返回值: true if both secure strings are equal; otherwise, false.
public static bool IsNumber(string s, bool floatPoint)
result = StringHelper.IsNumber(s, floatPoint)
Determines whether the string represents a number.
- s
- The string to test.
- floatPoint
- If true, considers floating point numbers; otherwise, integers.
返回值: True if the string can be parsed as a number; otherwise, false.
public static bool IsNumberOnly(string s, bool floatPoint)
result = StringHelper.IsNumberOnly(s, floatPoint)
Determines whether the string contains only numeric characters (and optionally a decimal separator).
- s
- The string to test.
- floatPoint
- If true, allows decimal points or commas.
返回值: True if the string contains only numbers; otherwise, false.
public static bool IsValidEmailAddress(string email)
result = StringHelper.IsValidEmailAddress(email)
Determines whether the specified string is a valid email address.
- The email address to test.
返回值: True if the email address is valid; otherwise, false.
public static bool IsValidUrl(string url)
result = StringHelper.IsValidUrl(url)
Determines whether the specified string is a valid URL.
- url
- The URL to test.
返回值: True if the URL is valid; otherwise, false.
public static string Join(IEnumerable<string> parts, string separator)
result = StringHelper.Join(parts, separator)
Joins the collection of strings using the specified separator.
- parts
- The collection of strings to join.
- separator
- The separator string.
返回值: A string resulting from the join.
public static string JoinAnd(IEnumerable<string> parts)
result = StringHelper.JoinAnd(parts)
Joins the collection of strings using the ampersand as the separator.
- parts
- The collection of strings to join.
返回值: A string resulting from the join.
public static string JoinAt(IEnumerable<string> parts)
result = StringHelper.JoinAt(parts)
Joins the collection of strings using "@" as the separator.
- parts
- The collection of strings to join.
返回值: A string resulting from the join.
public static string JoinColon(IEnumerable<string> parts)
result = StringHelper.JoinColon(parts)
Joins the collection of strings using a colon as the separator.
- parts
- The collection of strings to join.
返回值: A string resulting from the join.
public static string JoinComma(IEnumerable<string> parts)
result = StringHelper.JoinComma(parts)
Joins the collection of strings using a comma as the separator.
- parts
- The collection of strings to join.
返回值: A string resulting from the join.
public static string JoinCommaSpace(IEnumerable<string> parts)
result = StringHelper.JoinCommaSpace(parts)
Joins the collection of strings using a comma and a space as the separator.
- parts
- The collection of strings to join.
返回值: A string resulting from the join.
public static string JoinDot(IEnumerable<string> parts)
result = StringHelper.JoinDot(parts)
Joins the collection of strings using a dot as the separator.
- parts
- The collection of strings to join.
返回值: A string resulting from the join.
public static string JoinDotComma(IEnumerable<string> parts)
result = StringHelper.JoinDotComma(parts)
Joins the collection of strings using a semicolon as the separator.
- parts
- The collection of strings to join.
返回值: A string resulting from the join.
public static string JoinEqual(IEnumerable<string> parts)
result = StringHelper.JoinEqual(parts)
Joins the collection of strings using an equal sign as the separator.
- parts
- The collection of strings to join.
返回值: A string resulting from the join.
public static string JoinN(IEnumerable<string> parts)
result = StringHelper.JoinN(parts)
Joins the collection of strings using the newline character as the separator.
- parts
- The collection of strings to join.
返回值: A string resulting from the join.
public static string JoinNL(IEnumerable<string> parts)
result = StringHelper.JoinNL(parts)
Joins the collection of strings using the system's newline as the separator.
- parts
- The collection of strings to join.
返回值: A string resulting from the join.
public static string JoinPipe(IEnumerable<string> parts)
result = StringHelper.JoinPipe(parts)
Joins the collection of strings using the pipe character as the separator.
- parts
- The collection of strings to join.
返回值: A string resulting from the join.
public static string JoinRN(IEnumerable<string> parts)
result = StringHelper.JoinRN(parts)
Joins the collection of strings using the carriage return and newline as the separator.
- parts
- The collection of strings to join.
返回值: A string resulting from the join.
public static string JoinSpace(IEnumerable<string> parts)
result = StringHelper.JoinSpace(parts)
Joins the collection of strings using a space as the separator.
- parts
- The collection of strings to join.
返回值: A string resulting from the join.
public static string JoinTab(IEnumerable<string> parts)
result = StringHelper.JoinTab(parts)
Joins the collection of strings using a tab character as the separator.
- parts
- The collection of strings to join.
返回值: A string resulting from the join.
public static int LastIndexOf(StringBuilder builder, char value)
result = StringHelper.LastIndexOf(builder, value)
Finds the last index of a specified character in a StringBuilder.
- builder
- The StringBuilder to search.
- value
- The character to locate.
返回值: The zero-based index position of the character if found; otherwise, -1.
public static int LastIndexOfIgnoreCase(string str1, string str2, int index)
result = StringHelper.LastIndexOfIgnoreCase(str1, str2, index)
Searches for the last occurrence of a specified string, ignoring case.
- str1
- The string to search within.
- str2
- The string to search for.
- index
- The starting position of the search. The search is conducted from this position to the beginning. Default is -1 (entire string).
返回值: The index of the last occurrence of str2 in str1, or -1 if not found or if either string is null.
public static string LightScreening(string text)
result = StringHelper.LightScreening(text)
Performs light screening on the input text by replacing spaces with hyphens and removing certain characters.
- text
- The input text.
返回值: The screened text.
public static bool Like(string toSearch, string toFind, bool ignoreCase)
result = StringHelper.Like(toSearch, toFind, ignoreCase)
Determines if the current string matches the specified pattern using SQL-like wildcards.
- toSearch
- The string to search in.
- toFind
- The pattern to search for. Use '_' for single character and '%' for multiple characters.
- ignoreCase
- if set to true, the comparison ignores case.
返回值: true if the string matches the pattern; otherwise, false.
public static string Nl2Br(string s)
result = StringHelper.Nl2Br(s)
Replaces newline characters in the string with HTML line breaks.
- s
- The string to modify.
返回值: The modified string with HTML <br /> tags in place of newlines.
public static string Put(string str, object[] args)
result = StringHelper.Put(str, args)
Formats the string using standard string.Format with the provided arguments.
- str
- The format string.
- args
- The arguments to format the string with.
返回值: A formatted string.
public static string PutEx(string str, object[] args)
result = StringHelper.PutEx(str, args)
Formats the string using Smart.Format with the provided arguments.
- str
- The format string.
- args
- The arguments to format the string with.
返回值: A formatted string.
public static SecureString ReadOnly(SecureString str)
result = StringHelper.ReadOnly(str)
Makes the SecureString read-only.
- str
- The SecureString.
返回值: The read-only SecureString.
public static string Reduce(string s, int count, string endings)
result = StringHelper.Reduce(s, count, endings)
Reduces the string to a specified length and appends the specified ending.
- s
- The string to reduce.
- count
- The total length of the returned string including the ending.
- endings
- The ending to append.
返回值: A reduced version of the string.
public static string Remove(string s, string what, bool ignoreCase)
result = StringHelper.Remove(s, what, ignoreCase)
Removes all occurrences of the specified substring from the string.
- s
- The string from which to remove.
- what
- The substring to remove.
- ignoreCase
- If true, performs a case-insensitive removal.
返回值: The modified string.
public static string RemoveDiacritics(string s)
result = StringHelper.RemoveDiacritics(s)
Removes diacritical marks from the string.
- s
- The string from which to remove diacritics.
返回值: The modified string without accents.
public static void RemoveLast(StringBuilder builder, int count)
StringHelper.RemoveLast(builder, count)
Removes the specified number of characters from the end of the StringBuilder.
- builder
- The StringBuilder to modify.
- count
- The number of characters to remove.
public static string RemoveMultipleWhitespace(string text)
result = StringHelper.RemoveMultipleWhitespace(text)
Removes multiple consecutive whitespace characters from the specified text.
- text
- The input text.
返回值: A string with multiple whitespaces replaced by a single space.
public static string RemoveSpaces(string s)
result = StringHelper.RemoveSpaces(s)
Removes all spaces from the string.
- s
- The string from which to remove spaces.
返回值: The string without spaces.
public static string RemoveTrailingZeros(string s, string separator)
result = StringHelper.RemoveTrailingZeros(s, separator)
Removes trailing zeros from the string based on the specified separator.
- s
- The input string.
- separator
- The separator to consider while removing zeros.
返回值: A string with trailing zeros removed.
public static string RemoveTrailingZeros(string s)
result = StringHelper.RemoveTrailingZeros(s)
Removes trailing zeros from the string using the current thread's number format decimal separator.
- s
- The input string.
返回值: A string with trailing zeros removed.
public static string ReplaceIgnoreCase(string original, string oldValue, string newValue)
result = StringHelper.ReplaceIgnoreCase(original, oldValue, newValue)
Replaces occurrences of a specified substring with another string, ignoring case.
- original
- The original string.
- oldValue
- The substring to replace.
- newValue
- The replacement string.
返回值: The modified string.
public static StringBuilder ReplaceIgnoreCase(StringBuilder builder, string oldValue, string newValue)
result = StringHelper.ReplaceIgnoreCase(builder, oldValue, newValue)
Replaces occurrences of a specified substring with another string in a StringBuilder, ignoring case.
- builder
- The StringBuilder to modify.
- oldValue
- The substring to replace.
- newValue
- The replacement string.
返回值: The modified StringBuilder.
public static string ReplaceWhiteSpaces(string s)
result = StringHelper.ReplaceWhiteSpaces(s)
Replaces all white space characters in the string with a single space.
- s
- The string in which to replace white spaces.
返回值: The modified string.
public static string ReplaceWhiteSpaces(string s, char c)
result = StringHelper.ReplaceWhiteSpaces(s, c)
Replaces all white space characters in the string with the specified character.
- s
- The string in which to replace white spaces.
- c
- The character to replace white spaces with.
返回值: The modified string.
public static string Reverse(string input)
result = StringHelper.Reverse(input)
Reverses the specified string.
- input
- The string to reverse.
返回值: The reversed string.
public static SecureString Secure(string str)
result = StringHelper.Secure(str)
Converts the string to a SecureString.
- str
- The input string.
返回值: The SecureString equivalent of the input string.
public static string[] SplitByAt(string str, bool removeEmptyEntries)
result = StringHelper.SplitByAt(str, removeEmptyEntries)
Splits the string using the "@" symbol as the separator.
- str
- The string to split.
- removeEmptyEntries
- If true, removes empty entries.
返回值: An array of substrings.
public static string[] SplitByColon(string str, bool removeEmptyEntries)
result = StringHelper.SplitByColon(str, removeEmptyEntries)
Splits the string using a colon as the separator.
- str
- The string to split.
- removeEmptyEntries
- If true, removes empty entries.
返回值: An array of substrings.
public static string[] SplitByComma(string str, bool removeEmptyEntries)
result = StringHelper.SplitByComma(str, removeEmptyEntries)
Splits the string using a comma as the separator.
- str
- The string to split.
- removeEmptyEntries
- If true, removes empty entries.
返回值: An array of substrings.
public static string[] SplitByDot(string str, bool removeEmptyEntries)
result = StringHelper.SplitByDot(str, removeEmptyEntries)
Splits the string using a dot as the separator.
- str
- The string to split.
- removeEmptyEntries
- If true, removes empty entries.
返回值: An array of substrings.
public static string[] SplitByDotComma(string str, bool removeEmptyEntries)
result = StringHelper.SplitByDotComma(str, removeEmptyEntries)
Splits the string using a semicolon as the separator.
- str
- The string to split.
- removeEmptyEntries
- If true, removes empty entries.
返回值: An array of substrings.
public static string[] SplitByEqual(string str, bool removeEmptyEntries)
result = StringHelper.SplitByEqual(str, removeEmptyEntries)
Splits the string using the equal sign as the separator.
- str
- The string to split.
- removeEmptyEntries
- If true, removes empty entries.
返回值: An array of substrings.
public static IEnumerable<string> SplitByLength(string stringToSplit, int length)
result = StringHelper.SplitByLength(stringToSplit, length)
Splits the specified string into substrings of the given length.
- stringToSplit
- The string to split.
- length
- The maximum length of each substring.
返回值: An enumerable collection of substrings.
public static string[] SplitByLineSeps(string str, bool removeEmptyEntries)
result = StringHelper.SplitByLineSeps(str, removeEmptyEntries)
Splits the string by line separators (RN, R, or N).
- str
- The string to split.
- removeEmptyEntries
- If true, removes empty entries from the result.
返回值: An array of substrings.
public static string[] SplitByN(string str, bool removeEmptyEntries)
result = StringHelper.SplitByN(str, removeEmptyEntries)
Splits the string using the newline character as the separator.
- str
- The string to split.
- removeEmptyEntries
- If true, removes empty entries.
返回值: An array of substrings.
public static string[] SplitByR(string str, bool removeEmptyEntries)
result = StringHelper.SplitByR(str, removeEmptyEntries)
Splits the string using the carriage return as the separator.
- str
- The string to split.
- removeEmptyEntries
- If true, removes empty entries.
返回值: An array of substrings.
public static string[] SplitByRN(string str, bool removeEmptyEntries)
result = StringHelper.SplitByRN(str, removeEmptyEntries)
Splits the string using the carriage return and newline as the separator.
- str
- The string to split.
- removeEmptyEntries
- If true, removes empty entries.
返回值: An array of substrings.
public static string[] SplitBySep(string str, string separator, bool removeEmptyEntries)
result = StringHelper.SplitBySep(str, separator, removeEmptyEntries)
Splits the string by the specified separator.
- str
- The string to split.
- separator
- The separator string.
- removeEmptyEntries
- If true, removes empty entries.
返回值: An array of substrings.
public static string[] SplitBySpace(string str, bool removeEmptyEntries)
result = StringHelper.SplitBySpace(str, removeEmptyEntries)
Splits the string using a space as the separator.
- str
- The string to split.
- removeEmptyEntries
- If true, removes empty entries.
返回值: An array of substrings.
public static string[] SplitByTab(string str, bool removeEmptyEntries)
result = StringHelper.SplitByTab(str, removeEmptyEntries)
Splits the string using a tab character as the separator.
- str
- The string to split.
- removeEmptyEntries
- If true, removes empty entries.
返回值: An array of substrings.
public static bool StartsWithIgnoreCase(string str1, string str2)
result = StringHelper.StartsWithIgnoreCase(str1, str2)
Determines whether the string starts with the specified substring, ignoring case.
- str1
- The string to test.
- str2
- The substring to compare.
返回值: True if str1 starts with str2 ignoring case; otherwise, false.
public static string StripBrackets(string str, string sStart, string sEnd)
result = StringHelper.StripBrackets(str, sStart, sEnd)
Removes the enclosing brackets from the string if present.
- str
- The input string.
- sStart
- The starting bracket string.
- sEnd
- The ending bracket string.
返回值: The string with brackets removed, if they were present.
public static string ThrowIfEmpty(string str, string paramName)
result = StringHelper.ThrowIfEmpty(str, paramName)
Throws ArgumentNullException if the string is null or empty.
- str
- The string to check.
- paramName
- The parameter name to use in the exception.
返回值: The original string if not empty.
public static SecureString ThrowIfEmpty(SecureString str, string paramName)
result = StringHelper.ThrowIfEmpty(str, paramName)
Throws an ArgumentNullException if the secure string is empty.
- str
- The secure string.
- paramName
- The name of the parameter.
返回值: The original secure string if not empty.
public static string Times(string value, int n, string separator)
result = StringHelper.Times(value, n, separator)
Repeats the string a specified number of times with a separator between each repetition.
- value
- The string to repeat.
- n
- The number of times to repeat the string.
- separator
- The string to use as a separator between repetitions.
返回值: A new string containing the original string repeated n times with the specified separator.
public static string Times(string value, int n)
result = StringHelper.Times(value, n)
Repeats the string a specified number of times.
- value
- The string to repeat.
- n
- The number of times to repeat the string.
返回值: A new string containing the original string repeated n times.
public static string ToBitString(byte[] buffer, int? index, int? count, char separator)
result = StringHelper.ToBitString(buffer, index, count, separator)
Converts a byte array to its bit string representation with a specified separator.
- buffer
- The byte array.
- index
- Optional start index for conversion.
- count
- Optional count of bytes to convert.
- separator
- The character separator between byte bits.
返回值: A string representing the bits of the bytes.
public static byte[] ToByteArray(string bitString, char separator)
result = StringHelper.ToByteArray(bitString, separator)
Converts a bit string representation to a byte array.
- bitString
- The string containing bits.
- separator
- The character that separates individual byte bits.
返回值: A byte array created from the bit string.
public static int? ToId(SecureString key)
result = StringHelper.ToId(key)
Converts a SecureString key to a numeric identifier.
- key
- The secure key.
返回值: A numeric identifier or null if the key is null.
public static string ToLatin(string russianTitle)
result = StringHelper.ToLatin(russianTitle)
Transliterates the Russian title to Latin characters.
- russianTitle
- The Russian string.
返回值: The transliterated string in Latin.
public static char ToLower(char c, bool invariant)
result = StringHelper.ToLower(c, invariant)
Converts the specified character to lowercase.
- c
- The character to convert.
- invariant
- If set to true, uses culture-invariant conversion; otherwise, uses current culture.
返回值: The lowercase equivalent of the character.
public static string ToString(char[] arr, int count, int index)
result = StringHelper.ToString(arr, count, index)
Converts a character array to a string using the specified count and starting index.
- arr
- The character array.
- count
- The number of characters to convert.
- index
- The starting index in the array.
返回值: A new string constructed from the array.
public static string ToString(char[] arr, uint count, int index)
result = StringHelper.ToString(arr, count, index)
Converts a character array to a string using the specified count and starting index.
- arr
- The character array.
- count
- The number of characters to convert.
- index
- The starting index in the array.
返回值: A new string constructed from the array.
public static string ToTitleCase(string value)
result = StringHelper.ToTitleCase(value)
Converts the string to title case using the current culture.
- value
- The string to convert.
返回值: The string converted to title case.
public static char ToUpper(char c, bool invariant)
result = StringHelper.ToUpper(c, invariant)
Converts the specified character to uppercase.
- c
- The character to convert.
- invariant
- If set to true, uses culture-invariant conversion; otherwise, uses current culture.
返回值: The uppercase equivalent of the character.
public static string Trim(string value, int maxLength)
result = StringHelper.Trim(value, maxLength)
Trims the string to the specified maximum length and appends "..." if it exceeds that length.
- value
- The string to trim.
- maxLength
- The maximum length of the returned string.
返回值: A trimmed version of the string.
public static string TrimEnd(string str, string sEndValue)
result = StringHelper.TrimEnd(str, sEndValue)
Trims the specified end value from the end of the string if present.
- str
- The input string.
- sEndValue
- The ending substring to remove.
返回值: The trimmed string.
public static string TrimStart(string str, string sStartValue)
result = StringHelper.TrimStart(str, sStartValue)
Trims the specified start value from the beginning of the string if present.
- str
- The input string.
- sStartValue
- The starting substring to remove.
返回值: The trimmed string.
public static string Truncate(string text, int maxLength, string suffix)
result = StringHelper.Truncate(text, maxLength, suffix)
Truncates the string to a specified length and appends a custom suffix if truncated.
- text
- The string to truncate.
- maxLength
- The maximum length of the resulting string before adding the suffix.
- suffix
- The string to append if truncation occurs.
返回值: The truncated string with the suffix if truncation occurred.
public static string Truncate(string text, int maxLength)
result = StringHelper.Truncate(text, maxLength)
Truncates the string to a specified length and appends "..." if truncated.
- text
- The string to truncate.
- maxLength
- The maximum length of the resulting string.
返回值: The truncated string.
public static string TruncateMiddle(string input, int limit)
result = StringHelper.TruncateMiddle(input, limit)
Truncates a string in the middle, preserving the start and end portions while adding an ellipsis in between.
- input
- The string to truncate.
- limit
- The maximum length of the resulting string including the ellipsis.
返回值: The truncated string with ellipsis in the middle if truncation was necessary, otherwise the original string.
public static long? TryToLong(string str)
result = StringHelper.TryToLong(str)
Tries to parse the string to a long integer.
- str
- The string to parse.
返回值: The long value if successfully parsed; otherwise, null.
public static string Unicode(byte[] v, int index, int count)
result = StringHelper.Unicode(v, index, count)
Gets the string from a portion of the byte array using Unicode encoding.
- v
- The byte array.
- index
- The starting index.
- count
- The number of bytes to decode.
返回值: The decoded string.
public static int Unicode(ReadOnlySpan<char> v, Span<byte> destination)
result = StringHelper.Unicode(v, destination)
Encodes a span of characters into a span of bytes using Unicode encoding.
- v
- The span of characters to encode.
- destination
- The destination span to write the encoded bytes.
返回值: The number of bytes written to the destination.
public static string Unicode(ReadOnlySpan<byte> v)
result = StringHelper.Unicode(v)
Gets the string from a span of bytes using Unicode encoding.
- v
- The span of bytes.
返回值: The Unicode decoded string.
public static byte[] Unicode(string v)
result = StringHelper.Unicode(v)
Gets the Unicode encoded bytes for the string.
- v
- The input string.
返回值: The Unicode byte array.
public static string Unicode(byte[] v)
result = StringHelper.Unicode(v)
Gets the string from a byte array using Unicode encoding.
- v
- The byte array.
返回值: The Unicode decoded string.
public static string Unicode(byte[] v, uint count, int index)
result = StringHelper.Unicode(v, count, index)
Gets the string from a byte array using Unicode encoding with a specified count.
- v
- The byte array.
- count
- The number of bytes to decode.
- index
- The starting index.
返回值: The decoded string.
public static string UnSecure(SecureString str)
result = StringHelper.UnSecure(str)
Converts the SecureString to an unsecured string.
- str
- The SecureString.
返回值: The unsecured string equivalent.
private static Regex UrlValidationRegex()
result = StringHelper.UrlValidationRegex()
public static byte[] UTF8(string v)
result = StringHelper.UTF8(v)
Gets the UTF8 encoded bytes for the string.
- v
- The input string.
返回值: The UTF8 byte array.
public static string UTF8(byte[] v)
result = StringHelper.UTF8(v)
Gets the string from a byte array using UTF8 encoding.
- v
- The byte array.
返回值: The UTF8 decoded string.
public static string UTF8(byte[] v, int index, int count)
result = StringHelper.UTF8(v, index, count)
Gets the string from a portion of the byte array using UTF8 encoding.
- v
- The byte array.
- index
- The starting index.
- count
- The number of bytes to decode.
返回值: The decoded string.
public static string UTF8(byte[] v, uint count, int index)
result = StringHelper.UTF8(v, count, index)
Gets the string from a byte array using UTF8 encoding with a specified count.
- v
- The byte array.
- count
- The number of bytes to decode.
- index
- The starting index.
返回值: The decoded string.
public static int UTF8(ReadOnlySpan<char> v, Span<byte> destination)
result = StringHelper.UTF8(v, destination)
Encodes a span of characters into a span of bytes using UTF8 encoding.
- v
- The span of characters to encode.
- destination
- The destination span to write the encoded bytes.
返回值: The number of bytes written to the destination.
public static string UTF8(ReadOnlySpan<byte> v)
result = StringHelper.UTF8(v)
Gets the string from a span of bytes using UTF8 encoding.
- v
- The span of bytes.
返回值: The UTF8 decoded string.
字段
public static readonly HexEncoding HexEncoding
value = StringHelper.HexEncoding
Provides hexadecimal encoding functionality.
public const string RN
value = StringHelper.RN
Represents the carriage return and newline characters "\r\n".