-
Notifications
You must be signed in to change notification settings - Fork 2
String extensions
#####StringExtensions.Append
Given:
string one = "one";
string two = "two";
Instead of:
string.Concat(one, two) == "onetwo";
We can write:
one.Append(two) == "onetwo";
Other overloads of Append allow the following:
one.Append(two, 2) == "onetwotwo"
one.Append("two", "three") == "onetwothree"
one.Append(new[]{"two", "three", "four"}) == "onetwothreefour"
#####StringExtensions.AppendNewLine
Given:
var one = "one";
We can write:
one.AppendNewLine() == "one\r\n"
one.AppendNewLine(2) == "one\r\n\r\n"
#####StringExtensions.ContainsAll
Given:
var value = "one two three four";
Instead of:
if (value.Contains("one") && value.Contains("three"))
{
//do something
}
We can write:
if (value.ContainsAll("one", "three"))
{
//do something
}
#####StringExtensions.ContainsAny
Given:
var value = "one two three four";
Instead of:
if (value.Contains("one") || value.Contains("three"))
{
//do something
}
We can write:
if (value.ContainsAny("one", "three"))
{
//do something
}
#####StringExtensions.FormatWith
Given:
var template = "1 {0} 2 {1}";
Instead of:
string.Format(template, "one", "two") == "1 one 2 two";
We can write:
template.FormatWith("one", "two") == "1 one 2 two";
#####StringExtensions.HasValue
Given a null or empty string:
var value == null;
//or
var value == string.Empty;
Instead of:
var hasValue = value == null || value == string.Empty;
//or using NExtensions
var hasValue = !value.IsNullOrEmpty();
We can write:
var hasvalue = value.HasValue();
#####StringExtensions.IsNullOrEmpty
Instead of:
if (string.IsNullOrEmpty(user.Username))
{
//do something...
}
We can now write:
if (user.Username.IsNullOrEmpty())
{
//do something...
}
#####StringExtensions.IsNullOrWhiteSpace
Instead of:
if (string.IsNullOrWhiteSpace(user.Username))
{
//do something...
}
We can now write:
if (user.Username.IsNullOrWhiteSpace())
{
//do something...
}
#####StringExtensions.JoinWith and variants
Given:
var values = new []{"one", "two", "three"};
Instead of:
string.Join(values, "/") == "one/two/three";
We can write:
values.JoinWith("/") == "one/two/three";
And some shortcut variants:
values.JoinWithComma() == "one,two,three";
values.JoinWithComma(StringJoinOptions.AddSpace) == "one, two, three";
values.JoinWithSemiColon() == "one;two;three";
values.JoinWithSemiColon(StringJoinOptions.AddSpace) == "one; two; three";
values.JoinWithNewLine() == "one\r\ntwo\r\nthree";
#####StringExtensions.Remove
Given:
var value = "onetwothree";
Instead of:
value.Replace("two", string.Empty) == "onethree";
We can write:
value.Remove("two") == "onethree";
And instead of:
value.Replace("one", string.Empty).Replace("three", string.Empty) == "two";
We can write:
value.Remove("one", "three") == "two";
#####StringExtensions.SplitBy and variants
Given:
var value = "one/ two/three/ ";
Instead of:
var values = string.Split(new{"/"}, StringSplitOptions.RemoveEmptyEntries);
We can write:
var values = value.SplitBy("/");
Which both result in:
values.Length == 4
values[0] == "one"
values[1] == " two"
values[2] == "three"
values[3] == " "
But with the SplitBy extension method we can also write this:
var values = value.SplitBy("/", StringSplitOptions.TrimWhiteSpaceAndRemoveEmptyEntries);
Which then results in this:
values.Length == 3
values[0] == "one"
values[1] == "two"
values[2] == "three"
Other variants which are hopefully self explanatory are:
var values = value.SplitByComma();
var values = value.SplitBySemiColon();
var values = value.SpliyByNewLine();
All the above variants also accept an optional options parameter with which we can specify whether to remove empty entries and / or to trim the split string values.
#####StringExtensions.ToBoolean
Given:
var value = "true";
Instead of:
Convert.ToBoolean(value);
We can write:
value.ToBoolean();
ToBoolean supports all case variants of the following strings:
"true"
"false"
"on"
"off"
"0"
"1"
#####StringExtensions.ToDecimal
Given:
var value = "12.345";
Instead of:
decimal result;
if (decimal.TryParse(value, out result))
return result;
else
throw new FormatException("Couldn't convert value '{0}' to a Decimal".FormatWith(value))
We can write:
value.ToDecimal();
ToDecimal supports all the following number string formats:
"123.45"
"12,345.67"
"(123.45)"
"-" == 0
"123.45%" == 1.2345
"1.845E-07" == 0.0000001845
"1.845e-07" == 0.0000001845
#####StringExtensions.ToInteger
Given:
var value = "1,234";
Instead of:
(int) Convert.ToDecimal(value)
We can write:
value.ToInteger();
ToInteger supports all the following number string formats:
"123"
"12,345"
"(123)"
"-" == 0