Asp.NET Tutorials
Home > Asp.Net开发 > C# String Library

Hot archives

C# String Library
by Chad Finsterwald

Overview:

recently I was doing some coding and switching back and forth between a project written in PHP and another in C#. Having rebuilt my PHP muscle memory after a couple months of neglect, I instinctively called nl2br on a string in C#. Obviously instead of the desired result, I got a compliation error. Like a tiny bolt of lightning, it struck me that there are a number of useful functions in the PHP string library that are missing in C#'s implentation of String. This library is the result of that wee epiphany.

the intent of the article is to introduce you to the PHP inspired String Library for .Net. I did not port over all the PHP String functions --as some of the methods in PHP's library are a little too exotic for my taste --such as the levenshtein function. or the even more questionable str_rot13. I am just not sure when I'd need those, so I left them out. (In fact, if anyone has any examples of when they used str_rot13, please let me know!) I also added a few methods that are not in PHP's library but that I thought would be helpful --e.g. TrimIntraWords, FilterWords, etc.

Methods

below is a list of the method names and descriptions in the string library. I did not give the signatures, but many of these have several overloads. Also, I did not keep the PHP method names as those names do not conform to .Net best practices. You can view all the methods, their overloads, and documentation by clicking on the "View the Code" button below or by just downloading the .cs file.

  • Base64StringEncode: Base64 encodes a string.
  • Base64StringDecode: Decodes a Base64 string.
  • CaseInsenstiveReplace: A case insenstive replace function.
  • FilterWords: Removes all the words passed in the filter words parameters. The replace is NOT case sensitive.
  • HasWords: Checks the passed string to see if it contains any of the passed words. Not case-sensitive.
  • HtmlSpecialEntitiesEncode: A wrapper around HttpUtility.HtmlEncode.
  • HtmlSpecialEntitiesDecode: A wrapper around HttpUtility.HtmlDecode.
  • MD5String: MD5 encodes the passed string.
  • MD5VerifyString: Verifies a string against the passed MD5 hash.
  • PadLeftHtmlSpaces: Left pads the passed string using the HTML non-breaking space ( ) for the total number of spaces.
  • CaseInsenstiveReplace: Performs a case insenstive replace.
  • PadLeft: Left pads the passed string using the passed pad string for the total number of spaces. (It will not cut-off the pad even if it causes the string to exceed the total width.)
  • PadRightHtmlSpaces: Right pads the passed string using the HTML non-breaking space ( ) for the total number of spaces.
  • PadRight: Right pads the passed string using the passed pad string for the total number of spaces. (It will not cut-off the pad even if it causes the string to exceed the total width.)
  • RemoveNewLines: Removes the new line (\n) and carriage return (\r) symbols.
  • Reverse: Reverses the passed string.
  • SentenceCase: Converts a string to sentence case.
  • SpaceToNbsp: Converts all spaces to HTML non-breaking spaces.
  • StripTags: Removes all HTML tags from the passed string.
  • TitleCase: Converts a string to title case.
  • TrimIntraWords: Removes multiple spaces between words.
  • NewLineToBreak: Converts each new line (\n) and carriage return (\r) symbols to the HTML
    tag.
  • WordWrap: Wraps the passed string at the passed total number of characters (if cuttOff is true)or at the next whitespace (if cutOff is false). Uses the environment new linesymbol for the break text.

 
C# String Library:
using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Web; using System.Xml; namespace CoreWebLibrary.String { /// /// Helper functions for String not already found in C#. /// Inspired by PHP String Functions that are missing in .Net. /// public static class StringHelper { /// /// Base64 encodes a string. /// /// A string /// A base64 encoded string public static string Base64StringEncode(string input) { byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(input); return Convert.ToBase64String(encbuff); } /// /// Base64 decodes a string. /// /// A base64 encoded string /// A decoded string public static string Base64StringDecode(string input) { byte[] decbuff = Convert.FromBase64String(input); return System.Text.Encoding.UTF8.GetString(decbuff); } /// /// A case insenstive replace function. /// /// The string to examine. /// The value to replace. /// The new value to be inserted /// A string public static string CaseInsenstiveReplace(string input, string newValue, string oldValue) { Regex regEx = new Regex(oldValue, RegexOptions.IgnoreCase | RegexOptions.Multiline); return regEx.Replace(input, newValue); } /// /// Removes all the words passed in the filter words parameters. The replace is NOT case /// sensitive. /// /// The string to search. /// The words to repace in the input string. /// A string. public static string FilterWords(string input, params string[] filterWords) { return StringHelper.FilterWords(input, char.MinValue, filterWords); } /// /// Removes all the words passed in the filter words parameters. The replace is NOT case /// sensitive. /// /// The string to search. /// A character that is inserted for each /// letter of the replaced word. /// The words to repace in the input string. /// A string. public static string FilterWords(string input, char mask, params string[] filterWords) { string stringMask = mask == char.MinValue ? string.Empty : mask.ToString(); string totalMask = stringMask; foreach (string s in filterWords) { Regex regEx = new Regex(s, RegexOptions.IgnoreCase | RegexOptions.Multiline); if (stringMask.Length > 0) { for (int i = 1; i < s.Length; i++) totalMask += stringMask; } input = regEx.Replace(input, totalMask); totalMask = stringMask; } return input; } /// /// Checks the passed string to see if has any of the passed words. Not case-sensitive. /// /// The string to check. /// The words to check for. /// A collection of the matched words. public static MatchCollection HasWords(string input, params string[] hasWords) { StringBuilder sb = new StringBuilder(hasWords.Length + 50); //sb.Append("["); foreach (string s in hasWords) { sb.AppendFormat("({0})|", StringHelper.HtmlSpecialEntitiesEncode(s.Trim())); } string pattern = sb.ToString(); pattern = pattern.TrimEnd('|'); // +"]"; Regex regEx = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline); return regEx.Matches(input); } /// /// A wrapper around HttpUtility.HtmlEncode /// /// The string to be encoded /// An encoded string public static string HtmlSpecialEntitiesEncode(string input) { return HttpUtility.HtmlEncode(input); } /// /// A wrapper around HttpUtility.HtmlDecode /// /// The string to be decoded /// The decode string public static string HtmlSpecialEntitiesDecode(string input) { return HttpUtility.HtmlDecode(input); } /// /// MD5 encodes the passed string /// /// The string to encode. /// An encoded string. public static string MD5String(string input) { // Create a new instance of the MD5CryptoServiceProvider object. MD5 md5Hasher = MD5.Create(); // Convert the input string to a byte array and compute the hash. byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input)); // Create a new Stringbuilder to collect the bytes // and create a string. StringBuilder sBuilder = new StringBuilder(); // Loop through each byte of the hashed data // and format each one as a hexadecimal string. for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } // Return the hexadecimal string. return sBuilder.ToString(); } /// /// Verified a string against the passed MD5 hash. /// /// The string to compare. /// The hash to compare against. /// True if the input and the hash are the same, false otherwise. public static bool MD5VerifyString(string input, string hash) { // Hash the input. string hashOfInput = StringHelper.MD5String(input); // Create a StringComparer an comare the hashes. StringComparer comparer = StringComparer.OrdinalIgnoreCase; if (0 == comparer.Compare(hashOfInput, hash)) { return true; } else { return false; } } /// /// Left pads the passed input using the HTML non-breaking string entity ( ) /// for the total number of spaces. /// /// The string to pad. /// The total number to pad the string. /// A padded string. public static string PadLeftHtmlSpaces(string input, int totalSpaces) { string space = " "; return PadLeft(input, space, totalSpaces * space.Length); } /// /// Left pads the passed input using the passed pad string /// for the total number of spaces. It will not cut-off the pad even if it /// causes the string to exceed the total width. /// /// The string to pad. /// The string to uses as padding. /// The total number to pad the string. /// A padded string. public static string PadLeft(string input, string pad, int totalWidth) { return StringHelper.PadLeft(input, pad, totalWidth, false); } /// /// Left pads the passed input using the passed pad string /// for the total number of spaces. It will cut-off the pad so that /// the string does not exceed the total width. /// /// The string to pad. /// The string to uses as padding. /// The total number to pad the string. /// A padded string. public static string PadLeft(string input, string pad, int totalWidth, bool cutOff) { if (input.Length >= totalWidth) return input; int padCount = pad.Length; string paddedString = input; while (paddedString.Length < totalWidth) { paddedString += pad; } // trim the excess. if (cutOff) paddedString = paddedString.Substring(0, totalWidth); return paddedString; } /// /// Right pads the passed input using the HTML non-breaking string entity ( ) /// for the total number of spaces. /// /// The string to pad. /// The total number to pad the string. /// A padded string. public static string PadRightHtmlSpaces(string input, int totalSpaces) { string space = " "; return PadRight(input, space, totalSpaces * space.Length); } /// /// Right pads the passed input using the passed pad string /// for the total number of spaces. It will not cut-off the pad even if it /// causes the string to exceed the total width. /// /// The string to pad. /// The string to uses as padding. /// The total number to pad the string. /// A padded string. public static string PadRight(string input, string pad, int totalWidth) { return StringHelper.PadRight(input, pad, totalWidth, false); } /// /// Right pads the passed input using the passed pad string /// for the total number of spaces. It will cut-off the pad so that /// the string does not exceed the total width. /// /// The string to pad. /// The string to uses as padding. /// The total number to pad the string. /// A padded string. public static string PadRight(string input, string pad, int totalWidth, bool cutOff) { if (input.Length >= totalWidth) return input; string paddedString = string.Empty; while (paddedString.Length < totalWidth - input.Length) { paddedString += pad; } // trim the excess. if (cutOff) paddedString = paddedString.Substring(0, totalWidth - input.Length); paddedString += input; return paddedString; } /// /// Removes the new line (\n) and carriage return (\r) symbols. /// /// The string to search. /// A string public static string RemoveNewLines(string input) { return StringHelper.RemoveNewLines(input, false); } /// /// Removes the new line (\n) and carriage return (\r) symbols. /// /// The string to search. /// If true, adds a space (" ") for each newline and carriage /// return found. /// A string public static string RemoveNewLines(string input, bool addSpace) { string replace = string.Empty; if (addSpace) replace = " "; string pattern = @"[\r|\n]"; Regex regEx = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline); return regEx.Replace(input, replace); } /// /// Reverse a string. /// /// The string to reverse /// A string public static string Reverse(string input) { if (input.Length <= 1) return input; char[] c = input.ToCharArray(); StringBuilder sb = new StringBuilder(c.Length); for (int i = c.Length - 1; i > -1; i--) sb.Append(c[i]); return sb.ToString(); } /// /// Converts a string to sentence case. /// /// The string to convert. /// A string public static string SentenceCase(string input) { if (input.Length < 1) return input; string sentence = input.ToLower(); return sentence[0].ToString().ToUpper() + sentence.Substring(1); } /// /// Converts all spaces to HTML non-breaking spaces /// /// The string to convert. /// A string public static string SpaceToNbsp(string input) { string space = " "; return input.Replace(" ", space); } /// /// Removes all HTML tags from the passed string /// /// The string whose values should be replaced. /// A string. public static string StripTags(string input) { Regex stripTags = new Regex("<(.|\n)+?>"); return stripTags.Replace(input, ""); } /// /// Converts a string to title case. /// /// The string to convert. /// A string. public static string TitleCase(string input) { return TitleCase(input, true); } /// /// Converts a string to title case. /// /// The string to convert. /// If true, does not capitalize words like /// "a", "is", "the", etc. /// A string. public static string TitleCase(string input, bool ignoreShortWords) { List<string> ignoreWords = null; if (ignoreShortWords) { //TODO: Add more ignore words? ignoreWords = new List<string>(); ignoreWords.Add("a"); ignoreWords.Add("is"); ignoreWords.Add("was"); ignoreWords.Add("the"); } string[] tokens = input.Split(' '); StringBuilder sb = new StringBuilder(input.Length); foreach (string s in tokens) { if (ignoreShortWords == true && s != tokens[0] && ignoreWords.Contains(s.ToLower())) { sb.Append(s + " "); } else { sb.Append(s[0].ToString().ToUpper()); sb.Append(s.Substring(1).ToLower()); sb.Append(" "); } } return sb.ToString().Trim(); } /// /// Removes multiple spaces between words /// /// The string to trim. /// A string. public static string TrimIntraWords(string input) { Regex regEx = new Regex(@"[\s]+"); return regEx.Replace(input, " "); } /// /// Converts new line(\n) and carriage return(\r) symbols to /// HTML line breaks. /// /// The string to convert. /// A string. public static string NewLineToBreak(string input) { Regex regEx = new Regex(@"[\n|\r]+"); return regEx.Replace(input, "
"); } /// /// Wraps the passed string at the /// at the next whitespace on or after the total charCount has been reached /// for that line. Uses the environment new line /// symbol for the break text. /// /// The string to wrap. /// The number of characters per line. /// A string. public static string WordWrap(string input, int charCount) { return StringHelper.WordWrap(input, charCount, false, Environment.NewLine); } /// /// Wraps the passed string at the total number of characters (if cuttOff is true) /// or at the next whitespace (if cutOff is false). Uses the environment new line /// symbol for the break text. /// /// The string to wrap. /// The number of characters per line. /// If true, will break in the middle of a word. /// A string. public static string WordWrap(string input, int charCount, bool cutOff) { return StringHelper.WordWrap(input, charCount, cutOff, Environment.NewLine); } /// /// Wraps the passed string at the total number of characters (if cuttOff is true) /// or at the next whitespace (if cutOff is false). Uses the passed breakText /// for lineBreaks. /// /// The string to wrap. /// The number of characters per line. /// If true, will break in the middle of a word. /// The line break text to use. /// A string. public static string WordWrap(string input, int charCount, bool cutOff, string breakText) { StringBuilder sb = new StringBuilder(input.Length + 100); int counter = 0; if (cutOff) { while (counter < input.Length) { if (input.Length > counter + charCount) { sb.Append(input.Substring(counter, charCount)); sb.Append(breakText); } else { sb.Append(input.Substring(counter)); } counter += charCount; } } else { string[] strings = input.Split(' '); for (int i = 0; i < strings.Length; i++) { counter += strings[i].Length + 1; // added one to represent the space. if (i != 0 && counter > charCount) { sb.Append(breakText); counter = 0; } sb.Append(strings[i] + ' '); } } return sb.ToString().TrimEnd(); // to get rid of the extra space at the end. } } }

Examples

below are some examples of the methods. As most are fairly self-explanatory, I've only included a few.

reverse: 'CoreWeb' to 'beWeroC'
TitleCase: 'this is a test' to 'This is a Test'
SentenceCase: 'this is a test' to 'This is a test'
PadLeftHtmlSpaces: 'CoreWeb' to                         'CoreWeb'
MD5String: 'Finsterwald' to 'c7ea6dc934da143bcf00d14f722d74ea'
CaseInsenstiveReplace: 'This is a Test' to 'This is a Result'

i think you get the idea...

Conclusion

i hope that you found this helpful. I look foward to your comments and if you extend the library, please send me what you've done.

Add by : Huobazi (2006-8-27:04:52)