C# Programming > Data

C# Text Processing
Function Library

String Functions in C#.NET

C#.NET comes with a lot of great string processing functions like Substring, Compare, IndexOf. But the truth is the built-in .NET string functions are very limited. Programmers constantly have to rewrite similar text processing functions over and over.

Luckily we can expand on them and create all kinds of C# advanced string functions. As for a matter of speed, there are is one thing to consider: String vs StringBuilder

.NET - String vs StringBuilder

Appending text to a string object is done in this fashion:

stringObject += "more text";

Concating strings like that is very fast and reliable when it is done for a small amount of C# strings. In fact it can be significantly simpler and faster than using the StringBuilder class.

The StringBuilder class on the other hand is excellent for processing text for a lot of iterations since it avoids creating new instances of a string each time the output string is modified. To add a string to a StringBuilder goes like this:

stringBuilderObject.Append("more text");

The downside to the StringBuilder is the overhead of initializing a class. However if we are going to need to create some C# functions that will processes large amounts of text, then StringBuilder saves processing time in the long run.

In the C# String Processing Library functions are divided into two sections, the StringProcessing and the StringBuilderProcess classes, so you can run whichever one is more fitting.

Advanced Csharp String Functions

Capitalize

This C# function takes the first letter of a string an capitalizes it:
word -> Word
this is a sentence -> This is a sentence

IsCapitalized

This C# function checks to see if the first letter of a string is capitalized:
Word -> True
word -> False

IsLowerCase

Checks to see that an entire string is in lower cases
word -> True
Word -> False

IsUpperCase

Checks to see that an entire string is in upper cases
Word -> False
WORD -> True

SwapCases

This C# function swaps the cases of a string
word -> WORD
Word -> wORD

AlternateCases

Takes the first character's casing an alternates the casing of the rest of the string
Hi -> Hi
helloworld -> hElLoWoRlD

AlternateCases    

This C# string function works exactly the same except the user specifies on which case the string will start (Upper case or Lower case)

IsAlternateCases

Checks to see whether a string has alternating cases

CountTotal

Counts the total number of occurances of a string within another string
hello, l -> 2
hello, el -> 1

RemoveVowels

This C# string function removes the vowels in a string
hello -> hll

KeepVowels

This C# string function removes everything but the vowels in a string
hello -> eo

HasVowels

Checks to see if there is any vowel psent in a string

IsSpaces

Quickly and effortlessly checks to see if a string is nothing but spaces

IsRepeatedChar

Quickly and effortlessly checks to see if a string is nothing but the same letter repeated
aaaaaaaaaa -> True
aaaaaaaaad -> False

IsNumeric

Processes a string to see if it contains only numbers

HasNumbers

Checks a string to see if it contains any numbers.

IsAlphaNumberic

This C# function evaluates whether a string contains only numbers and letters (no symbols).

isLetters

Checks for a string to contain nothing but letters, no numbers or symbols.

GetInitials

Converts a string, like a name, into its initials
Bob Landon -> B.L.

GetTitle

Capitalizes the first letter of every word in a string
the good story -> The Good Story

GetNameCasing

Similar to the GetTitle function, capitalizes the first letter of every word, but has some additional rules for names
mcdonald -> McDonald
macdonald -> MacDonald
Credits to ShutlOrbit (http://www.thirdstagesoftware.com) from CodeProject

IsTitle

This C# string function checks if the first letter of every word is capitalized
The Big Story -> True
The big story -> False

IsEmailAddress

Verifies that an email address is written in the correct format. Useful for checking email addressed entered in a web application.

IndexOfAll

This very useful C# function returns all the indicies of a string in another string. As opposed to IndexOf which only returns the first index.

ArrayToString

This C# string function is a must for all developers. Quickly turns any array into a single string that can be displayed to survey an array's data. Check out a more complete array to string function right here on Visual C# Kicks.

PasswordStrength

Evaluate the effectiveness of a string as a password. Original idea credits go to D. Rijmenants. (If there are any copyright issues please contact us).

CharRight

Basically a Substring function that works backwards. Programmers from older languages will appciate this missing C# function.

CharMid

Another function that is missing from the original C# Net string processing list. Works like Substring but starts from a specified position.

InsertSeparator

Inserts a separator after each letter in a string, excluding the last letter
hello, - -> h-e-l-l-o

InsertSeparatorEvery

Inserts a separator after a specified number of letters, excluding the last letter

SubstringEnd

This C# function works exactly like the built-in Substring. The only difference is it takes in a Start and End parameter instead of the default Start and Length. (Basically the Java version of Substring)

Reverse

Reverses a string without the need for a recursive function.

SplitQuotes

This C# function works like the built-in Split function. The only difference is it will respect parts of a string surrounded by quotes. For example the string This is a "very long" string would get split into:
This
is
a
very long
string

Careful however, the function does not work with nested quotes.

Downloadable Components

For a downloadable easy-to-use component version of the string functions above, visit the Visual C# Kicks String Processing Library .NET component. It is completely free and is in essence the next version of the functions above.

Using the Source Code

All the C# string functions in .NET have been optimized for speed and minimum resource use. Feel free to build upon them to write your own C#.NET functions. Download the C# source code to check out how they work.

Back to C# Article List