Introduction
The default .NET string library comes with several functions to manage strings that are quite helpful (Substring
, Compare
, IndexOf
, etc.). The problem is that the amount and variety of these functions is very limited. I find myself constantly needing string handling functions beyond those that are given, and I find myself constantly rewriting the same functions over and over again for new projects. So, I have put together a library of functions that I have found useful. Some are very basic and some are a little more complex, but they are all useful in my opinion.
Using the code
I won't post the actual source code here (it's attached) since there are a lot of functions and it would take up too much of space.
Functions for managing file paths
GetPath(<Full Path>)
The function retrieves the path part from the full filename like, C:\Folder\file.exe -> C:\Folder\.
GetName(<Full Path>)
It retrieves the name of the file from the full filename like, C:\Folder\file.exe - > file, file.exe -> file.
GetExtension(<Full Path>)
It retrieves the extension of the file from a filename C:\Folder\file.exe - > exe, file.exe -> exe.
RemoveExtension(<Full Path>)
It removes the extension from a filename like, C:\Folder\file.exe - > C:\Folder\file, file.exe -> file.
GetFullName(<Full Path>)
It removes the path from a filename like, C:\Folder\file.exe - > file.exe.
Other functions
IsCapitalized(<String>)
It checks whether a word or sentence is capitalized like, Word -> True, This is a sentence -> True.
IsLower(<String>)
It checks whether a word or character is in lower case like, word -> True, Word -> False.
IsUpper(<String>)
It checks whether a word or character is in upper case like, word -> False, Word -> False, WORD -> True.
SwapCases(<String>)
It swaps the cases in a string like, word -> WORD, Word -> wORD, WoRd -> wOrD.
AlternateCases(<String>)
It alternates the cases between letters of a string, the first letter's case stays the same like, Hi -> Hi, longstring -> lOnGsTrInG.
IsAlternateCases(<String>)
It checks to see if a string has alternate cases like, lOnGsTrInG -> True.
IsMixedCases(<String>)
It checks for mixed upper and lower cases like, string -> False, String -> True.
CountTotal(<String>, <Chars>)
It counts total number of a string or character in a string like, hello, l -> 2, hello, he -> 1.
RemoveVowels(<String>)
It removes vowels from a string like, remove -> rmv.
HasVowels(<String>)
It checks to see if a string contains vowels like, hello -> True, rmv -> False.
IsSpaces(<String>)
It checks if a string is nothing but spaces like, " " -> True.
IsNumeric(<String>)
It checks if a string has only numbers like, 12453 -> True, 234d3 -> False.
I know that there is already an existing IsNumeric
function, but the existing one takes the input parameter as an Object
, so in theory, this function should be faster for larger strings because it takes the input as a String
.
HasNumbers(<String>)
It checks if the string contains numbers like, hello -> False, h3llo -> True.
IsAlphaNumberic(<String>)
It checks if the string has numbers and letters like, Test1254 -> True, $chool -> False.
IsLetters(<String>)
It checks if the string contains only letters like, Hi -> True, Hi123 -> False.
Initials(<String>, <Capitalize>, <ReturnSeparator>, <Optional Separator = " ">)
It returns the initials of a name or sentence.
Capitalize
- To return the initials as lower or upper case.
ReturnSeparator
- To return the initials with the original separator (John Smith, a space is the separator, so return J. S. or J.S.).
Separator
- The character that marks the separation of words (John Smith, space is the separator (default)) John Smith -> J. S.
Title(<String>, <Optional Separator = " ">)
It capitalizes the first letter of every word in the string like, the big story -> The Big Story.
IsTitle(<String>, <Optional Separator = " ">)
It checks whether the first letter of each word is capitalized like, The Big Story -> True, The big story -> False.
IsEmailAddress(<String>)
It checks if a string is in a valid email address-format like, name@place.com -> True, hahaimfaking -> False. (The function works by assuming the last part is no bigger than 3 letters (com, net, org).)
IndexesOf(<String>, ByVal [Char] As String) As Integer()
It returns all the locations of a given char in a string like, Hello, l -> 2, 3, Hello, o -> 4.
This function works with an ArrayList
and then converts it to an Integer
array, if you prefer the ArrayList
it can be easily modified.
CharRight(<String>, <Position>) As Char
It gets the char in a string at a given position, but from right to left like, string, 0 -> g.
CharMid(<String>, <Position>, <Start>) As Char
It gets the char in a string at a given position from a given starting point, and reads from left to right like, string, 0, 2 -> r.
InsertSeparator(<String>, <Separator As String>)
It inserts a separator after every letter like, hello, "-" -> h-e-l-l-o.
InsertSeparatorEvery(<String>, <Separator>, <Count>)
It inserts a separator after every Count
letters like, hello, "-", 2 -> he-ll-o.
InsertSeparatorAt(<String>, <Separator> <Position >)
It inserts a separator at given position like, hello, "-", 3 -> hel-lo.
Substring(<String>, <Start>, <End>)
This is a function that works the same way as the default Substring
, but it takes Start
and End
parameters instead of Start
and Length
.
Reverse(<String>)
It reverses a string like, Hello -> olleH.
PasswordStrength(<String>) As Integer
It is a borrowed function. It returns a rating for how strong the string is as a password. (Max rating is 100).
The credits for the original function goes to D. Rijmenants, this is just a VB.NET version. If there is any problem with copyright or whatever, contact me and I'll delete this. This is just an interesting function that took me a long time to find, so I thought it'd be useful to include it here.
Split(<String>, <Optional Don
This function is newly added (on 9/27/05). Thanks to j1webb for the idea. This is just like the default Split
function, but it keeps everything inside the quotes, intact. For example: 'This is a "very long" string' would return 'This', 'is', 'a', 'very long', 'string'. A limitation of this function is that if there's a quote inside quotes it will mess up. The quote symbol is " by default, but it can be changed to look for ' instead.
Points of interest
Most of these functions are set up to return empty strings or something of that effect when there is an invalid situation, to prevent runtime errors, but feel free to modify them to handle invalid strings in other ways. Some of these functions are dependant on other functions, so make sure to add all the needed functions to your project or merge the needed ones. For functions where a new string was built from the original string, I have used the class StringBuilder
since it is much faster when it comes to those bigger strings, if you do not intend to handle large strings then you might want to adjust the function to use a regular String
type instead. These functions are not the fastest I'm sure. I wrote them as best as I could, but I'm sure there is room for improvement and optimization.
Conclusion
Some of these functions seem almost pointless, but I have found them to be very useful at some point or the other. Feel free to do whatever you want with these functions, no annoying copyright or anything like that.