Enhanced C#
Language of your choice: library documentation
Static Public Member Functions | List of all members
Loyc.StringExt Class Reference

Extension methods for strings, such as SplitAt, Left, Right, FormatCore and Slice. More...


Source file:

Remarks

Extension methods for strings, such as SplitAt, Left, Right, FormatCore and Slice.

Static Public Member Functions

static string WithoutPrefix (this string s, string prefix, StringComparison mode=StringComparison.Ordinal)
 Returns a version of the string without the specified prefix. If the string does not have the specified prefix, it is returned unchanged. The prefix check is case-sensitive. More...
 
static string WithoutSuffix (this string s, string suffix, StringComparison mode=StringComparison.Ordinal)
 Returns a version of the string without the specified suffix. If the string does not have the specified suffix, it is returned unchanged. The suffix check is case-sensitive. More...
 
static UString WithoutPrefix (this UString s, UString prefix, bool ignoreCase=false)
 Returns a version of the string without the specified prefix. If the string does not have the specified prefix, it is returned unchanged. The prefix check is case-sensitive. More...
 
static UString WithoutSuffix (this UString s, UString suffix, bool ignoreCase=false)
 Returns a version of the string without the specified suffix. If the string does not have the specified suffix, it is returned unchanged. The suffix check is case-sensitive. More...
 
static Pair< UString, UStringSplitAt (this string s, char delimiter)
 Gets the substrings to the left and right of a dividing character. More...
 
static Pair< UString, UStringSplitAt (this string s, string delimiter)
 
static bool Contains (this string s, char c)
 
static string Right (this string s, int count)
 Returns the rightmost 'count' characters of 's', or s itself if count > s.Length. More...
 
static string Left (this string s, int count)
 Returns the leftmost 'count' characters of 's', or s itself if count > s.Length. More...
 
static ? char TryGet (this string s, int index)
 
static char TryGet (this string s, int index, char defaultValue)
 
static string SafeSubstring (this string s, int startIndex, int length=int.MaxValue)
 A variation on String.Substring() that never throws. More...
 
static string Join (string separator, IEnumerable value)
 Converts a series of values to strings, and concatenates them with a given separator between them. More...
 
static string Join (string separator, IEnumerator value)
 
static UString Slice (this string str, int start, int count=int.MaxValue)
 
static UString Find (this string str, UString what, bool ignoreCase=false)
 
static string FormatCore (this string format, params object[] args)
 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. More...
 
static string EliminateNamedArgs (string format, params object[] args)
 Called by Format to replace named placeholders with numeric placeholders in format strings. More...
 

Member Function Documentation

◆ EliminateNamedArgs()

static string Loyc.StringExt.EliminateNamedArgs ( string  format,
params object[]  args 
)
inlinestatic

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

Returns
A format string that can be used to call string.Format.
See also
FormatCore

References Loyc.UString.Slice().

Referenced by Loyc.StringExt.FormatCore().

◆ FormatCore()

static string Loyc.StringExt.FormatCore ( this string  format,
params object[]  args 
)
inlinestatic

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.

References Loyc.StringExt.EliminateNamedArgs().

◆ Join()

static string Loyc.StringExt.Join ( string  separator,
IEnumerable  value 
)
inlinestatic

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

Join(" + ", new[] { 1,2,3 }) returns "1 + 2 + 3".

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

References Loyc.StringExt.Join().

Referenced by Loyc.StringExt.Join().

◆ Left()

static string Loyc.StringExt.Left ( this string  s,
int  count 
)
inlinestatic

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

◆ Right()

static string Loyc.StringExt.Right ( this string  s,
int  count 
)
inlinestatic

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

◆ SafeSubstring()

static string Loyc.StringExt.SafeSubstring ( this string  s,
int  startIndex,
int  length = int.MaxValue 
)
inlinestatic

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

◆ SplitAt()

static Pair<UString, UString> Loyc.StringExt.SplitAt ( this string  s,
char  delimiter 
)
inlinestatic

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

Parameters
sString to split
delimiterDividing character.
Returns
Returns the string to the left and to the right of the first occurance of 'c' in the string, not including 'c' itself. If 'c' was not found in 's', the pair (s, null) is returned.

◆ WithoutPrefix() [1/2]

static string Loyc.StringExt.WithoutPrefix ( this string  s,
string  prefix,
StringComparison  mode = StringComparison.Ordinal 
)
static

Returns a version of the string without the specified prefix. If the string does not have the specified prefix, it is returned unchanged. The prefix check is case-sensitive.

◆ WithoutPrefix() [2/2]

static UString Loyc.StringExt.WithoutPrefix ( this UString  s,
UString  prefix,
bool  ignoreCase = false 
)
static

Returns a version of the string without the specified prefix. If the string does not have the specified prefix, it is returned unchanged. The prefix check is case-sensitive.

◆ WithoutSuffix() [1/2]

static string Loyc.StringExt.WithoutSuffix ( this string  s,
string  suffix,
StringComparison  mode = StringComparison.Ordinal 
)
static

Returns a version of the string without the specified suffix. If the string does not have the specified suffix, it is returned unchanged. The suffix check is case-sensitive.

◆ WithoutSuffix() [2/2]

static UString Loyc.StringExt.WithoutSuffix ( this UString  s,
UString  suffix,
bool  ignoreCase = false 
)
static

Returns a version of the string without the specified suffix. If the string does not have the specified suffix, it is returned unchanged. The suffix check is case-sensitive.