C# String Library

by Chad Finsterwald

This code sample applies to ASP.NET for the .NET 2.0 framework. Examples are written in C#. Download the code

Contents

  1. Overview
  2. Methods
  3. Examples
  4. Conclusion

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.
  • ReplaceFirst: Replaces the first occurence of a string with the replacement value. The Replace is case senstive. [The inclusion of this method and ReplaceLast was suggested by a reader's comment.]
  • ReplaceLast: Replaces the last occurence of a string with the replacement value. The replace is case senstive.
  • 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 <br /> 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.
Click to View the Code

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.

Comments

Subject Name Date Submitted
Change page: 1 2   |  Displaying page 1 of 2, items 1 to 20 of 35.
C# String Library - Excellent Article
rajarajan9/2/2006 3:30:09 PM
C# String Library - Excellent Article
rajarajan9/2/2006 3:30:33 PM
C# String Library
Kalel9/12/2006 8:26:54 AM
Good Article
vikram9/19/2006 1:33:46 AM
What about ReplaceFirst ?
Peter9/19/2006 2:53:57 AM
re: What about ReplaceFirst ?
Chad Finsterwald9/19/2006 12:54:37 PM
re: What about ReplaceFirst ?
Chad Finsterwald9/19/2006 8:19:50 PM
A helpful contribution
Ahmad9/19/2006 5:38:54 AM
re: A helpful contribution
Chad Finsterwald9/19/2006 12:57:29 PM
re: A helpful contribution
Ahmad9/21/2006 4:55:13 AM
awesome idea
dinah9/19/2006 2:57:16 PM
re: awesome idea
Joe9/26/2006 7:16:42 AM
suggestion
dinah9/20/2006 8:51:24 AM
re: suggestion
Chad Finsterwald9/20/2006 10:20:47 AM
very helpful, but..
db9/20/2006 11:31:55 AM
re: very helpful, but..
Chad Finsterwald9/20/2006 11:40:35 AM
re: very helpful, but.. update
Chad Finsterwald9/20/2006 11:54:00 AM
re: very helpful, but..
db9/20/2006 2:22:49 PM
something for the world
Jon9/20/2006 12:35:08 PM
re: something for the world
Joe9/26/2006 7:08:39 AM
New Comment
(Your email address will not be displayed or shared.)
Please enter the code shown below. If you cannot read it, press "reset image" to generate a new one.