C# Net comes with a lot of great string processing functions like Substring, Compare, IndexOf. But the truth is the built-in text functions are very limited.
Luckily we can expand on them and create all kinds of C# string processing functions. As for a matter of speed, there are is one thing to consider: String vs StringBuilder
Appending text a string object is done in this fashion:
stringObject += "more text";
Concating strings like that is very fast and reliable when it comes to small 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 large pieces of text. To add a string to a StringBuilder goes like this:
stringBuilderObject.Append("more text");
The downside to the StringBuilder is at the end it needs to be converted back into a regular string. However if we are going to need to create some C# functions that will processes big amounts of text, then StringBuilder is the way to go. For more on the speed performace of string vs string builder visit the application optimization article.
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.
public static string Capitalize(string input)
This C# function takes the first letter of a string an capitalizes it:
word -> Word
this is a sentence -> This is a sentence
public static bool IsCapitalized(string input)
This C# function checks to see if the first letter of a string is capitalized:
Word -> True
word -> False
public static bool IsLowerCase(string input)
Checks to see that an entire string is in lower cases
word -> True
Word -> False
public static bool IsUpperCase(string input)
Checks to see that an entire string is in upper cases
Word -> False
WORD -> True
public static string SwapCases(string input)
This C# function swaps the cases of a string
word -> WORD
Word -> wORD
public static string AlternateCases(string input)
Takes the first character's casing an alternates the casing of the rest of the string
Hi -> Hi
helloworld -> hElLoWoRlD
public static string AlternateCases(string input, bool firstIsUpper)
This C# string function works exactly the same except the user specifies on which case the string will start (Upper case or Lower case)
public static bool IsAlternateCases(string input)
Checks to see whether a string has alternating cases
public static int CountTotal(string input, string chars, bool ignoreCases)
Counts the total number of occurances of a string within another string
hello, l -> 2
hello, el -> 1
public static string RemoveVowels(string input)
This C# string function removes the vowels in a string
hello -> hll
public static string KeepVowels(string input)
This C# string function removes everything but the vowels in a string
hello -> eo
public static bool HasVowels(string input)
Checks to see if there is any vowel present in a string
public static bool IsSpaces(string input)
Quickly and effortlessly checks to see if a string is nothing but spaces
public static bool IsRepeatedChar(string input)
Quickly and effortlessly checks to see if a string is nothing but the same letter repeated
aaaaaaaaaa -> True
aaaaaaaaad -> False
public static bool IsNumeric(string input)
Processes a string to see if it contains only numbers
public static bool HasNumbers(string input)
Checks a string to see if it contains any numbers.
public static bool IsAlphaNumberic(string input)
This C# function evaluates whether a string contains only numbers and letters (no symbols).
public static bool isLetters(string input)
Checks for a string to contain nothing but letters, no numbers or symbols.
public static string GetInitials(string input, bool capitalize, bool includeSpace)
Converts a string, like a name, into its initials
Bob Landon -> B.L.
public static string GetTitle(string input)
Capitalizes the first letter of every word in a string
the good story -> The Good Story
public static string GetNameCasing(string input)
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
public static bool IsTitle(string input)
This C# string function checks if the first letter of every word is capitalized
The Big Story -> True
The big story -> False
public static bool IsEmailAddress(string input)
Verifies that an email address is written in the correct format. Useful for checking email addressed entered in a web application.
public static int[] IndexOfAll(string input, string chars)
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.
public static string ArrayToString(Array input, string separator)
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.
public static int PasswordStrength(string input)
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).
public static char CharRight(string input, int index)
Basically a Substring function that works backwards. Programmers from older languages will appreciate this missing C# function.
public static char CharMid(string input, int startingIndex, int countIndex)
Another function that is missing from the original C# Net string processing list. Works like Substring but starts from a specified position.
public static string InsertSeparator(string input, string separator)
Inserts a separator after each letter in a string, excluding the last letter
hello, - -> h-e-l-l-o
public static string InsertSeparatorEvery(string input, string separator, int count)
Inserts a separator after a specified number of letters, excluding the last letter
public static string SubstringEnd(string input, int start, int end)
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)
public static string Reverse(string input)
Reverses a string without the need for a recursive function.
public static string[] SplitQuotes(string input, bool ignoreQuotes, string separator)
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.
All the C#.Net string functions have been optimized for speed and minimum resource use. Feel free to build upon them to write your own C# functions. Download the C# source code to check out how they work...
Download All the Text Functions C# Source Code