Usage Examples: StringExtensions.cs

Oct 30, 2009 at 5:39 PM
Edited Oct 30, 2009 at 5:59 PM

StringExtensions.cs

Base64Decode

string encoded = "Hello, World!".Base64Encode();
string decoded = encoded.Base64Decode();

Base64Encode

string encoded = "Hello, World!".Base64Encode();
string decoded = encoded.Base64Decode();

CharacterInstanceCount

string value = "Hello, World!";
string character = "l";
int charCount = value.CharacterInstanceCount(character);

Contains

string s = "Hello, World!";
bool valid = s.Contains("hello", true); -> returns false

ContainsWords

string s = "This is a test sentence.";
string[] wordArray = new string[] { "test", "sentence" };
MatchCollection actual = s.ContainsWords(wordArray);
foreach (Match match in actual)
{
     GroupCollection groups = match.Groups;
     if (groups[0].Success == true)
     {
         Console.WriteLine("Found a match at index: " + groups[0].Index);
     }
}

Decrypt

string s = "Hello, World!";
string key = "mykey";
string encrypted = s.Encrypt(key);
string decrypted = encrypted.Decrypt(key);

Encrypt

string s = "Hello, World!";
string key = "mykey";
string encrypted = s.Encrypt(key);
string decrypted = encrypted.Decrypt(key);

EndsWithAny

string s = "Hello, World!";
List<string> values = new List<string>();
values.AddRange("hello", "!", "World");
bool result = s.EndsWithAny(values);

EndsWithAny

string s = "Hello, World!";
List<string> values = new List<string>();
values.AddRange("hello", "!", "World");
bool result = s.EndsWithAny(values, false);

EndsWithAny

string s = "Hello, World!";
List<string> values = new List<string>();
values.AddRange("hello", "!", "World");
bool result = s.EndsWithAny(values, true, CultureInfo.CurrentCulture);

FilterWords

string s = "This is a test sentence.";
string[] filterWords = new string[] { " is a", " sentence" };
string results = s.FilterWords(filterWords); -> results in "This test."

FilterWords

string s = "This is a test sentence.";
char mask = '*';
string[] filterWords = new string[] { "test" };
actual = s.FilterWords(mask, filterWords); -> results in "This is a **** sentence."

FindBetween

string s = "Hello, World!";
string startString = "";
string endString = "";
MatchCollection matches = s.FindBetween(startString, endString);
string firstMatch;
if (matches.Count > 0)
{
     firstMatch = matches[0].ToString();
}

FindBetween

string s = "Hello, World!";
string startString = "";
string endString = "";
MatchCollection matches = s.FindBetween(startString, endString, true);
string firstMatch;
if (matches.Count > 0)
{
     firstMatch = matches[0].ToString();
}

ForcePrefix

string s = " World!";
string prefix = "Hello,";
string results = s.ForcePrefix(prefix); -> results in "Hello, World!"

ForceSuffix

string s = "Hello, ";
string suffix = "World!";
string results = s.ForceSuffix(suffix); -> results in "Hello, World!"

FormatPhoneNumber

string formatted = "4065557485".FormatPhoneNumber();

IsDate

bool isDate = "12/31/1971".IsDate();

IsEmpty

if (!myString.IsEmpty())
{
     doStuff(myString);
}

IsNullOrEmpty

if (!myString.IsNullOrEmpty())
{
     doStuff(myString);
}

IsNumeric

if (myString.IsNumeric())
{
     int myInt = myString.ToInt32();
}

IsValidEmailAddress

if (TextEmail.Text.IsValidEmailAddress())
{
     ProcessRegistration();
}

IsValidIPAddress

bool isValid = ipAddress.IsValidIPAddress();

IsValidUrl

bool isValid = TextBoxUrl.Text.IsValidUrl();

Left

string s = "Hello, World!";
int charCount = 5;
string result = s.Left(charCount); // results in "Hello"

Mid

string s = "Hello, World!";
string results = s.Mid(2, 4);

PadLeft

string s = "Hello, World!";
string pad = " How are you?";
string results = s.PadLeft(pad);

PadLeft

string s = "Hello, World!";
string pad = " How are you?";
string actual = s.PadLeft(pad, 17, true);

PadRight

string s = "How are you?";
string pad = "Hello, World! ";
string results = s.PadRight(pad);

PadRight

string s = "How are you?";
string pad = "Hello, World! ";
string results = s.PadRight(pad, 17, true);

RemoveDuplicateWords

string expected = "To be or not - that is the question.";
string results = s.RemoveDuplicateWords();

RemoveNewLines

string s = "Hello, \r\nWorld!";
string results = s.RemoveNewLines();

RemoveNewLines

string s = "Hello, \r\nWorld!";
string results = s.RemoveNewLines(true);

RemovePrefix

string s = "Hello, World!";
string results = s.RemovePrefix("Hello, ");

RemoveSpaces

string phoneNumber = "555 555 5555";
string trimmed = phoneNumber.RemoveSpaces();

RemoveSuffix

string s = "Hello, World!";
string results = s.RemoveSuffix(", World!");

Replace

string s = "Hello, World!";
string results = s.Replace("world", "Dude", false);

Reverse

string s = "Hello, World!";
string results = s.Reverse();

Right

string s = "Hello, World!";
string results = s.Right(6);

SentenceCase

string s = "This Is Just a Test.";
string results = s.SentenceCase();

Slice

string s = "Hello, World!";
string results = s.Slice(0, 5);

SlugDecode

string s = "Hello_World!_How_are_you_today?";
string results = s.SlugDecode();

SlugEncode

string s = "Hello World! How are you today?";
string results = s.SlugEncode();

Split

string s = "one, two, three";
string[] results = s.Split(",");

Split

string s = "one, two, three";
string[] results = s.Split(",", true);

StartsWithAny

string s = "Hello, World!";
List<string> values = new List<string>();
values.Add("Goodbye");
values.Add("Hello");
bool results = s.StartsWithAny(values);

StartsWithAny

string s = "Hello, World!";
List<string> values = new List<string>();
values.Add("Goodbye");
values.Add("Hello");
bool results = s.StartsWithAny(values, true);

StartsWithAny

string s = "Hello, World!";
List<string> values = new List<string>();
values.Add("Goodbye");
values.Add("Hello");
bool results = s.StartsWithAny(values, true, CultureInfo.CurrentCulture);

StripTags

string s = "<body>Hello, World!</body>";
string results = s.StripTags();

TitleCase

string s = "On the waterfront";
string title = s.TitleCase();

TitleCase

string s = "On the waterfront";
string title = s.TitleCase(false);

ToBoolean

string s = "False";
bool results = s.ToBoolean();

ToByteArray

string s = "Hello, World!";
byte[] converted = s.ToByteArray();

ToColor

string s = "#ffffff";
Color expected = s.ToColor();

ToDecimal

Decimal result = "5".ToDecimal();

ToInt16

int number = "5".ToInt16();

ToInt32

int number = "5".ToInt32();

ToInt64

int number = "294967295".ToInt64();

TrimIntraWords

string s = "Hello, World!";
string trimmed = s.TrimIntraWords();

Truncate

string s = "Hello, World!";
string results = s.Truncate(5);

WordCount

string s = "Hello, World!";
int count = s.WordCount();

WordInstanceCount

string s = "Hello, World! How are you doing today?";
string word = "How";
int count = s.WordInstanceCount(word);

WordWrap

string s = "Hello, World! How are you doing today?";
string results = s.WordWrap(9);

WordWrap

string s = "Hello, World! How are you doing today?";
string results = s.WordWrap(9, false);

WordWrap

string s = "Hello, World! How are you doing today?";
string results = s.WordWrap(9, false, Environment.NewLine);