Table of Contents

Class AbpStringExtensions

Namespace
System
Assembly
Volo.Abp.Core.dll

Extension methods for String class.

public static class AbpStringExtensions
Inheritance
AbpStringExtensions
Inherited Members

Methods

EnsureEndsWith(string, char, StringComparison)

Adds a char to end of given string if it does not ends with the char.

public static string EnsureEndsWith(this string str, char c, StringComparison comparisonType = StringComparison.Ordinal)

Parameters

str string
c char
comparisonType StringComparison

Returns

string

EnsureStartsWith(string, char, StringComparison)

Adds a char to beginning of given string if it does not starts with the char.

public static string EnsureStartsWith(this string str, char c, StringComparison comparisonType = StringComparison.Ordinal)

Parameters

str string
c char
comparisonType StringComparison

Returns

string

GetBytes(string)

Converts given string to a byte array using UTF8 encoding.

public static byte[] GetBytes(this string str)

Parameters

str string

Returns

byte[]

GetBytes(string, Encoding)

Converts given string to a byte array using the given encoding

public static byte[] GetBytes(this string str, Encoding encoding)

Parameters

str string
encoding Encoding

Returns

byte[]

IsNullOrEmpty(string?)

Indicates whether this string is null or an System.String.Empty string.

public static bool IsNullOrEmpty(this string? str)

Parameters

str string

Returns

bool

IsNullOrWhiteSpace(string?)

indicates whether this string is null, empty, or consists only of white-space characters.

public static bool IsNullOrWhiteSpace(this string? str)

Parameters

str string

Returns

bool

Left(string, int)

Gets a substring of a string from beginning of the string.

public static string Left(this string str, int len)

Parameters

str string
len int

Returns

string

Exceptions

ArgumentNullException

Thrown if str is null

ArgumentException

Thrown if len is bigger that string's length

NormalizeLineEndings(string)

Converts line endings in the string to NewLine.

public static string NormalizeLineEndings(this string str)

Parameters

str string

Returns

string

NthIndexOf(string, char, int)

Gets index of nth occurrence of a char in a string.

public static int NthIndexOf(this string str, char c, int n)

Parameters

str string

source string to be searched

c char

Char to search in str

n int

Count of the occurrence

Returns

int

RemovePostFix(string, StringComparison, params string[])

Removes first occurrence of the given postfixes from end of the given string.

public static string RemovePostFix(this string str, StringComparison comparisonType, params string[] postFixes)

Parameters

str string

The string.

comparisonType StringComparison

String comparison type

postFixes string[]

one or more postfix.

Returns

string

Modified string or the same string if it has not any of given postfixes

RemovePostFix(string, params string[])

Removes first occurrence of the given postfixes from end of the given string.

public static string RemovePostFix(this string str, params string[] postFixes)

Parameters

str string

The string.

postFixes string[]

one or more postfix.

Returns

string

Modified string or the same string if it has not any of given postfixes

RemovePreFix(string, StringComparison, params string[])

Removes first occurrence of the given prefixes from beginning of the given string.

public static string RemovePreFix(this string str, StringComparison comparisonType, params string[] preFixes)

Parameters

str string

The string.

comparisonType StringComparison

String comparison type

preFixes string[]

one or more prefix.

Returns

string

Modified string or the same string if it has not any of given prefixes

RemovePreFix(string, params string[])

Removes first occurrence of the given prefixes from beginning of the given string.

public static string RemovePreFix(this string str, params string[] preFixes)

Parameters

str string

The string.

preFixes string[]

one or more prefix.

Returns

string

Modified string or the same string if it has not any of given prefixes

ReplaceFirst(string, string, string, StringComparison)

public static string ReplaceFirst(this string str, string search, string replace, StringComparison comparisonType = StringComparison.Ordinal)

Parameters

str string
search string
replace string
comparisonType StringComparison

Returns

string

Right(string, int)

Gets a substring of a string from end of the string.

public static string Right(this string str, int len)

Parameters

str string
len int

Returns

string

Exceptions

ArgumentNullException

Thrown if str is null

ArgumentException

Thrown if len is bigger that string's length

Split(string, string)

Uses string.Split method to split given string by given separator.

public static string[] Split(this string str, string separator)

Parameters

str string
separator string

Returns

string[]

Split(string, string, StringSplitOptions)

Uses string.Split method to split given string by given separator.

public static string[] Split(this string str, string separator, StringSplitOptions options)

Parameters

str string
separator string
options StringSplitOptions

Returns

string[]

SplitToLines(string)

Uses string.Split method to split given string by NewLine.

public static string[] SplitToLines(this string str)

Parameters

str string

Returns

string[]

SplitToLines(string, StringSplitOptions)

Uses string.Split method to split given string by NewLine.

public static string[] SplitToLines(this string str, StringSplitOptions options)

Parameters

str string
options StringSplitOptions

Returns

string[]

ToCamelCase(string, bool, bool)

Converts PascalCase string to camelCase string.

public static string ToCamelCase(this string str, bool useCurrentCulture = false, bool handleAbbreviations = false)

Parameters

str string

String to convert

useCurrentCulture bool

set true to use current culture. Otherwise, invariant culture will be used.

handleAbbreviations bool

set true to if you want to convert 'XYZ' to 'xyz'.

Returns

string

camelCase of the string

ToEnum<T>(string)

Converts string to enum value.

public static T ToEnum<T>(this string value) where T : struct

Parameters

value string

String value to convert

Returns

T

Returns enum object

Type Parameters

T

Type of enum

ToEnum<T>(string, bool)

Converts string to enum value.

public static T ToEnum<T>(this string value, bool ignoreCase) where T : struct

Parameters

value string

String value to convert

ignoreCase bool

Ignore case

Returns

T

Returns enum object

Type Parameters

T

Type of enum

ToKebabCase(string, bool)

Converts given PascalCase/camelCase string to kebab-case.

public static string ToKebabCase(this string str, bool useCurrentCulture = false)

Parameters

str string

String to convert.

useCurrentCulture bool

set true to use current culture. Otherwise, invariant culture will be used.

Returns

string

ToMd5(string)

public static string ToMd5(this string str)

Parameters

str string

Returns

string

ToPascalCase(string, bool)

Converts camelCase string to PascalCase string.

public static string ToPascalCase(this string str, bool useCurrentCulture = false)

Parameters

str string

String to convert

useCurrentCulture bool

set true to use current culture. Otherwise, invariant culture will be used.

Returns

string

PascalCase of the string

ToSentenceCase(string, bool)

Converts given PascalCase/camelCase string to sentence (by splitting words by space). Example: "ThisIsSampleSentence" is converted to "This is a sample sentence".

public static string ToSentenceCase(this string str, bool useCurrentCulture = false)

Parameters

str string

String to convert.

useCurrentCulture bool

set true to use current culture. Otherwise, invariant culture will be used.

Returns

string

ToSnakeCase(string)

Converts given PascalCase/camelCase string to snake case. Example: "ThisIsSampleSentence" is converted to "this_is_a_sample_sentence". https://github.com/npgsql/npgsql/blob/dev/src/Npgsql/NameTranslation/NpgsqlSnakeCaseNameTranslator.cs#L51

public static string ToSnakeCase(this string str)

Parameters

str string

String to convert.

Returns

string

Truncate(string?, int)

Gets a substring of a string from beginning of the string if it exceeds maximum length.

public static string? Truncate(this string? str, int maxLength)

Parameters

str string
maxLength int

Returns

string

TruncateFromBeginning(string?, int)

Gets a substring of a string from Ending of the string if it exceeds maximum length.

public static string? TruncateFromBeginning(this string? str, int maxLength)

Parameters

str string
maxLength int

Returns

string

TruncateWithPostfix(string?, int)

Gets a substring of a string from beginning of the string if it exceeds maximum length. It adds a "..." postfix to end of the string if it's truncated. Returning string can not be longer than maxLength.

public static string? TruncateWithPostfix(this string? str, int maxLength)

Parameters

str string
maxLength int

Returns

string

Exceptions

ArgumentNullException

Thrown if str is null

TruncateWithPostfix(string?, int, string)

Gets a substring of a string from beginning of the string if it exceeds maximum length. It adds given postfix to end of the string if it's truncated. Returning string can not be longer than maxLength.

public static string? TruncateWithPostfix(this string? str, int maxLength, string postfix)

Parameters

str string
maxLength int
postfix string

Returns

string

Exceptions

ArgumentNullException

Thrown if str is null