C# Class Loyc.StringExt

Extension methods for strings, such as SplitAt, Left, Right, Format and Slice.
显示文件 Open project: qwertie/ecsharp

Public Methods

Method Description
EliminateNamedArgs ( string format ) : string

Called by Format to replace named placeholders with numeric placeholders in format strings.

Find ( this str, UString what, bool ignoreCase = false ) : UString
FormatCore ( this format ) : string

This formatter works like string.Format, except that named placeholders accepted as well as numeric placeholders. This method replaces named placeholders with numbers, then calls string.Format.

Named placeholders are useful for communicating information about a placeholder to a human translator. Here is an example: Not enough memory to {load/parse} '{filename}'. In some cases a translator might have difficulty translating a phrase without knowing what a numeric placeholder ({0} or {1}) refers to, so a named placeholder can provide an important clue. The localization system is invoked as follows: string msg = "{man's name} meets {woman's name}.".Localized( "man's name", mansName, "woman's name", womansName); The placeholder names are not case sensitive. You can use numeric placeholders, alignment and formatting codes also: string msg = "You need to run {dist,6:###.00} km to reach {0}".Localized( cityName, "dist", 2.9); It is assumed that the placeholder name ends at the first comma or colon; hence the placeholder in this example is called "dist", not "dist,6:###.00". Typically, the named arguments are expected to start at index N+1 in the variable argument array, where {N} is the largest numeric placeholder, and if there are no numeric placeholders then the named arguments should begin at index 0. In this example there is a {0}, so the named arguments should start at index 1. However, since named arguments always come in pairs, an extra rule increments the N if the number of remaining arguments starting at N is not an even number. For example, in string msg = "Hello {0}, you'll go to {school name} next year.".Localized( firstName, lastName, "school name", schoolName); There are three args left after the numeric ones, so the first remaining argument is ignored to make it an even number. If a placeholder name is not found in the argument list then it is not replaced with a number before the call to string.Format, so a FormatException will occur.

Join ( string separator, IEnumerable value ) : string

Converts a series of values to strings, and concatenates them with a given separator between them.

This method (but taking IEnumerable{T}) exists in the BCL starting in .NET 4

Join ( string separator, IEnumerator value ) : string
Left ( this s, int count ) : string

Returns the leftmost 'count' characters of 's', or s itself if count > s.Length.

Right ( this s, int count ) : string

Returns the rightmost 'count' characters of 's', or s itself if count > s.Length.

SafeSubstring ( this s, int startIndex, int length = int.MaxValue ) : string

A variation on String.Substring() that never throws.

This is best explained by examples: "Hi everybody!".SafeSubstring(8, 500) == "body!" "Hi everybody!".SafeSubstring(-3, 5) == "Hi" "Hi everybody!".SafeSubstring(-5, 5) == "" "Hi everybody!".SafeSubstring(8, -5) == "" "Hi everybody!".SafeSubstring(500, 8) == "" "Hi everybody!".SafeSubstring(int.MinValue + 500, int.MaxValue) == "Hi everybody!" ((string)null).SafeSubstring(0, 1) == null

Slice ( this str, int start, int count = int.MaxValue ) : UString
SplitAt ( this s, char delimiter ) : UString>.Pair

Gets the substrings to the left and right of a dividing character.

SplitAt ( this s, string delimiter ) : UString>.Pair
TryGet ( this s, int index, char defaultValue ) : char
TryGet ( this s, int index ) : char?

Method Details

EliminateNamedArgs() public static method

Called by Format to replace named placeholders with numeric placeholders in format strings.
public static EliminateNamedArgs ( string format ) : string
format string
return string

Find() public static method

public static Find ( this str, UString what, bool ignoreCase = false ) : UString
str this
what UString
ignoreCase bool
return UString

FormatCore() public static method

This formatter works like string.Format, except that named placeholders accepted as well as numeric placeholders. This method replaces named placeholders with numbers, then calls string.Format.
Named placeholders are useful for communicating information about a placeholder to a human translator. Here is an example: Not enough memory to {load/parse} '{filename}'. In some cases a translator might have difficulty translating a phrase without knowing what a numeric placeholder ({0} or {1}) refers to, so a named placeholder can provide an important clue. The localization system is invoked as follows: string msg = "{man's name} meets {woman's name}.".Localized( "man's name", mansName, "woman's name", womansName); The placeholder names are not case sensitive. You can use numeric placeholders, alignment and formatting codes also: string msg = "You need to run {dist,6:###.00} km to reach {0}".Localized( cityName, "dist", 2.9); It is assumed that the placeholder name ends at the first comma or colon; hence the placeholder in this example is called "dist", not "dist,6:###.00". Typically, the named arguments are expected to start at index N+1 in the variable argument array, where {N} is the largest numeric placeholder, and if there are no numeric placeholders then the named arguments should begin at index 0. In this example there is a {0}, so the named arguments should start at index 1. However, since named arguments always come in pairs, an extra rule increments the N if the number of remaining arguments starting at N is not an even number. For example, in string msg = "Hello {0}, you'll go to {school name} next year.".Localized( firstName, lastName, "school name", schoolName); There are three args left after the numeric ones, so the first remaining argument is ignored to make it an even number. If a placeholder name is not found in the argument list then it is not replaced with a number before the call to string.Format, so a FormatException will occur.
public static FormatCore ( this format ) : string
format this
return string

Join() public static method

Converts a series of values to strings, and concatenates them with a given separator between them.
This method (but taking IEnumerable{T}) exists in the BCL starting in .NET 4
public static Join ( string separator, IEnumerable value ) : string
separator string
value IEnumerable
return string

Join() public static method

public static Join ( string separator, IEnumerator value ) : string
separator string
value IEnumerator
return string

Left() public static method

Returns the leftmost 'count' characters of 's', or s itself if count > s.Length.
public static Left ( this s, int count ) : string
s this
count int
return string

Right() public static method

Returns the rightmost 'count' characters of 's', or s itself if count > s.Length.
public static Right ( this s, int count ) : string
s this
count int
return string

SafeSubstring() public static method

A variation on String.Substring() that never throws.
This is best explained by examples: "Hi everybody!".SafeSubstring(8, 500) == "body!" "Hi everybody!".SafeSubstring(-3, 5) == "Hi" "Hi everybody!".SafeSubstring(-5, 5) == "" "Hi everybody!".SafeSubstring(8, -5) == "" "Hi everybody!".SafeSubstring(500, 8) == "" "Hi everybody!".SafeSubstring(int.MinValue + 500, int.MaxValue) == "Hi everybody!" ((string)null).SafeSubstring(0, 1) == null
public static SafeSubstring ( this s, int startIndex, int length = int.MaxValue ) : string
s this
startIndex int
length int
return string

Slice() public static method

public static Slice ( this str, int start, int count = int.MaxValue ) : UString
str this
start int
count int
return UString

SplitAt() public static method

Gets the substrings to the left and right of a dividing character.
public static SplitAt ( this s, char delimiter ) : UString>.Pair
s this String to split
delimiter char Dividing character.
return UString>.Pair

SplitAt() public static method

public static SplitAt ( this s, string delimiter ) : UString>.Pair
s this
delimiter string
return UString>.Pair

TryGet() public static method

public static TryGet ( this s, int index, char defaultValue ) : char
s this
index int
defaultValue char
return char

TryGet() public static method

public static TryGet ( this s, int index ) : char?
s this
index int
return char?