Back to Home
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:
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
This C# function takes the first letter of a string an capitalizes it:
word -> Word
this is a sentence -> This is a sentence
This C# function checks to see if the first letter of a string is capitalized:
Word -> True
word -> False
Checks to see that an entire string is in lower cases
word -> True
Word -> False
Checks to see that an entire string is in upper cases
Word -> False
WORD -> True
This C# function swaps the cases of a string
word -> WORD
Word -> wORD
Takes the first character’s casing an alternates the casing of the rest of the string
Hi -> Hi
helloworld -> hElLoWoRlD
This C# string function works exactly the same except the user specifies on which case the string will start (Upper case or Lower case)
Checks to see whether a string has alternating cases
Counts the total number of occurances of a string within another string
hello, l -> 2
hello, el -> 1
This C# string function removes the vowels in a string
hello -> hll
This C# string function removes everything but the vowels in a string
hello -> eo
Checks to see if there is any vowel psent in a string
Quickly and effortlessly checks to see if a string is nothing but spaces
Quickly and effortlessly checks to see if a string is nothing but the same letter repeated
aaaaaaaaaa -> True
aaaaaaaaad -> False
Processes a string to see if it contains only numbers
Checks a string to see if it contains any numbers.
This C# function evaluates whether a string contains only numbers and letters (no symbols).
Checks for a string to contain nothing but letters, no numbers or symbols.
Converts a string, like a name, into its initials
Bob Landon -> B.L.
Capitalizes the first letter of every word in a string
the good story -> The Good Story
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
This C# string function checks if the first letter of every word is capitalized
The Big Story -> True
The big story -> False
Verifies that an email address is written in the correct format. Useful for checking email addressed entered in a web application.
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.
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.
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).
Basically a Substring function that works backwards. Programmers from older languages will appciate this missing C# function.
Another function that is missing from the original C# Net string processing list. Works like Substring but starts from a specified position.
Inserts a separator after each letter in a string, excluding the last letter
hello, – -> h-e-l-l-o
Inserts a separator after a specified number of letters, excluding the last letter
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)
Reverses a string without the need for a recursive function.
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:
Careful however, the function does not work with nested quotes.