StringFormat

A suite of string formatting and validation methods.

CleanForSearch(this string input)

Console.WriteLine("Something en    français**".CleanForSearch()); // "SOMETHING EN FRANCAIS"

ConsolidateSpaces(this string input)

Console.WriteLine("  a b   c ".ConsolidateSpaces()); // "a b c"

EnsureLength(object input, int length)

Console.WriteLine(StringFormat.EnsureLength("12345", 4)); // "1234"
Console.WriteLine(StringFormat.EnsureLength(1.23456, 5)); // "1.234"

FilterNonDigits(this string input)

Console.WriteLine("abc123efg".FilterNonDigits()); // "123"
Console.WriteLine("abcdefghi".FilterNonDigits()); // ""

FormatAddress(...)

Formats an address with an input of either a MailingAddress instance or in separate string arguments. Uses the GeographyWizard to determine the name of the country, if GeographyWizard is available.

var address = new MailingAddress()
{
	Street1 = "One Microsoft Way",
	City = "Redmond",
	State = "WA",
	Postcode = "91245-3432",
	Country = "US"
};

Console.WriteLine(StringFormat.FormatAddress(address));
/*
	One Microsoft Way
	Redmond, WA
	United States 91245-3432
*/

FormatCreditCardNumber(this string input, string type)

Console.WriteLine(StringFormat.FormatCreditCardNumber("4111222233334444")); // "4111 2222 3333 4444"
Console.WriteLine(StringFormat.FormatCreditCardNumber(""3123 4567 8901 234")); // 3123 4567890 1234"

FormatName(this string input)

Console.WriteLine("april   o'neal   ".FormatName()); // "April O'Neal"

FormatPhone(this string input)

Console.WriteLine("519 5551234".FormatPhone()); // "(519) 555-1234"
Console.WriteLine("519 555 1234 555".FormatPhone()); // "(519) 555-1234 x555

GetFirstSentence(this string input), GetFirstSentence(this string input, int numberOfSentences)

var text = "First sentence. Second sentence, continued. Third sentence";

Console.WriteLine(text.GetFirstSentence()); // "First sentence."
Console.WriteLine(text.GetFirstsentence(2)); // "First sentence. Second sentence, continued."

GetHtmlReadyString(this string input)

var text = "First line\nSecond line\r\nThird line";

Console.WriteLine(text.GetHtmlReadyString()); // "First line<br/>Second line<br/>Third line"

GetUrlFriendlyString(this string input)

Console.WriteLine("Dr. Octopus's Orders".GetUrlFriendlyString()); // "dr-octopuss-orders"

IsInteger(this string input)

Console.WriteLine("abc 123".IsInteger()); // false
Console.WriteLine("123.456".IsInteger()); // false
Console.WriteLine("123".IsInteger()); // true
Console.WriteLine("  -1234  ".IsInteger()); true

IsNumber(this string input)

Console.WriteLine("abc 123".IsNumber()); // false
Console.WriteLine("123.456".IsNumber()); // true
Console.WriteLine("123".IsNumber()); // true
Console.WriteLine("  -1234  ".IsNumber()); true

IsValidEmail(this string input)

Console.WriteLine("aaa@bbb.com".IsValidEmail()); // true
Console.WriteLine("aaa@bbb.ccc".IsValidEmail()); // true
Console.WriteLine("  aaa@bbb.ccc ".IsValidEmail()); // true
Console.WriteLine("@bbb.ccc".IsValidEmail()); // false

NeutralizeAccents(this string input)

Console.WriteLine("en français"); // "en francais"

ReplaceIgnoreCase(this string input)

Console.WriteLine("Something About Code".ReplaceIgnoreCase("code", "bugs")); // "Something About bugs"

ReplaceSymbols(this string input)

Console.WriteLine("Hello #neighbor!!".ReplaceSymbols(" ")); // "Hello  neighbor  "

TitleCase(this string input), TitleCase(object input)

Console.WriteLine("title case".TitleCase()); // "Title Case"
Console.WriteLine(StringFormat.TitleCase(string.GetType()); // "String

ToNeutralString(object input)

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-CA");
var number = 1.234

Console.WriteLine(number); // "1,234"
Console.WriteLine(StringFormat.ToNeutralString(number)); // "1.234"

Last edited Aug 28, 2010 at 4:26 PM by weiwong, version 7

Comments

No comments yet.