Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / Visual Basic
Article

String Manipulation Library

Rate me:
Please Sign up or sign in to vote.
4.07/5 (19 votes)
27 Sep 2005CPOL6 min read 130.6K   910   47   25
Library of string manipulation functions.

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

VB
GetPath(<Full Path>)

The function retrieves the path part from the full filename like, C:\Folder\file.exe -> C:\Folder\.

VB
GetName(<Full Path>)

It retrieves the name of the file from the full filename like, C:\Folder\file.exe - > file, file.exe -> file.

VB
GetExtension(<Full Path>)

It retrieves the extension of the file from a filename C:\Folder\file.exe - > exe, file.exe -> exe.

VB
RemoveExtension(<Full Path>)

It removes the extension from a filename like, C:\Folder\file.exe - > C:\Folder\file, file.exe -> file.

VB
GetFullName(<Full Path>)

It removes the path from a filename like, C:\Folder\file.exe - > file.exe.

Other functions

VB
IsCapitalized(<String>)

It checks whether a word or sentence is capitalized like, Word -> True, This is a sentence -> True.

VB
IsLower(<String>)

It checks whether a word or character is in lower case like, word -> True, Word -> False.

VB
IsUpper(<String>)

It checks whether a word or character is in upper case like, word -> False, Word -> False, WORD -> True.

VB
SwapCases(<String>)

It swaps the cases in a string like, word -> WORD, Word -> wORD, WoRd -> wOrD.

VB
AlternateCases(<String>)

It alternates the cases between letters of a string, the first letter's case stays the same like, Hi -> Hi, longstring -> lOnGsTrInG.

VB
IsAlternateCases(<String>)

It checks to see if a string has alternate cases like, lOnGsTrInG -> True.

VB
IsMixedCases(<String>)

It checks for mixed upper and lower cases like, string -> False, String -> True.

VB
CountTotal(<String>, <Chars>)

It counts total number of a string or character in a string like, hello, l -> 2, hello, he -> 1.

VB
RemoveVowels(<String>)

It removes vowels from a string like, remove -> rmv.

VB
HasVowels(<String>)

It checks to see if a string contains vowels like, hello -> True, rmv -> False.

VB
IsSpaces(<String>)

It checks if a string is nothing but spaces like, " " -> True.

VB
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.

VB
HasNumbers(<String>)

It checks if the string contains numbers like, hello -> False, h3llo -> True.

VB
IsAlphaNumberic(<String>)

It checks if the string has numbers and letters like, Test1254 -> True, $chool -> False.

VB
IsLetters(<String>)

It checks if the string contains only letters like, Hi -> True, Hi123 -> False.

VB
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.
VB
Title(<String>, <Optional Separator = " ">)

It capitalizes the first letter of every word in the string like, the big story -> The Big Story.

VB
IsTitle(<String>, <Optional Separator = " ">)

It checks whether the first letter of each word is capitalized like, The Big Story -> True, The big story -> False.

VB
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).)

VB
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.

VB
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.

VB
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.

VB
InsertSeparator(<String>, <Separator As String>)

It inserts a separator after every letter like, hello, "-" -> h-e-l-l-o.

VB
InsertSeparatorEvery(<String>, <Separator>, <Count>)

It inserts a separator after every Count letters like, hello, "-", 2 -> he-ll-o.

VB
InsertSeparatorAt(<String>, <Separator> <Position >)

It inserts a separator at given position like, hello, "-", 3 -> hel-lo.

VB
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.

VB
Reverse(<String>)

It reverses a string like, Hello -> olleH.

VB
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.

VB
Split(<String>, <Optional Don't Account for Quotes>, <Optional Separator>)

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Visit Visual C# Kicks for more .Net Resources and Articles at vckicks.110mb.com

Comments and Discussions

 
GeneralMy vote of 5 Pin
ChintanHiremath21-Mar-12 18:34
ChintanHiremath21-Mar-12 18:34 
GeneralC# version of these functions Pin
gpmaker12-Apr-08 18:46
gpmaker12-Apr-08 18:46 
GeneralRe: C# version of these functions Pin
VCSKicks16-Dec-08 8:58
VCSKicks16-Dec-08 8:58 
Questionhow to get numbers from text.. Pin
Eng.hamzeh Abualrob24-Nov-07 23:16
Eng.hamzeh Abualrob24-Nov-07 23:16 
AnswerRe: how to get numbers from text.. Pin
VCSKicks31-Mar-08 18:27
VCSKicks31-Mar-08 18:27 
AnswerRe: how to get numbers from text.. Pin
Member 90411922-Sep-15 10:31
Member 90411922-Sep-15 10:31 
Generalthank you Pin
riquard10-Nov-07 14:28
riquard10-Nov-07 14:28 
very useful, thanks!
Generalthnx again.. Pin
BeBadgujar17-Jun-07 18:30
BeBadgujar17-Jun-07 18:30 
Questionthank you very much..!!!but..... Pin
BeBadgujar10-Jun-07 18:33
BeBadgujar10-Jun-07 18:33 
AnswerRe: thank you very much..!!!but..... Pin
gpmaker10-Jun-07 18:40
gpmaker10-Jun-07 18:40 
Questionhow to extrat the name of the folder? Pin
BeBadgujar8-Jun-07 22:58
BeBadgujar8-Jun-07 22:58 
AnswerRe: how to extrat the name of the folder? Pin
gpmaker10-Jun-07 10:43
gpmaker10-Jun-07 10:43 
GeneralAnother Needed Function: Name Case Pin
Geosynch26-Apr-07 20:04
Geosynch26-Apr-07 20:04 
QuestionSuite - or is it Sweet ?? Pin
stixoffire18-Mar-07 4:45
stixoffire18-Mar-07 4:45 
GeneralVB6 Shell function Pin
FIFI512-Mar-07 7:56
FIFI512-Mar-07 7:56 
GeneralGood job! Pin
Garret1177-Jul-06 3:28
Garret1177-Jul-06 3:28 
GeneralGreat Library Pin
ooKmonkey5-May-06 0:49
ooKmonkey5-May-06 0:49 
Questionany tip for parsing? Pin
kenkenkennek23-Apr-06 0:26
kenkenkennek23-Apr-06 0:26 
GeneralGood Work Pin
xoxoxoxoxoxox11-Jan-06 12:29
xoxoxoxoxoxox11-Jan-06 12:29 
GeneralCounting... Pin
CoolNetCoder22-Oct-05 4:55
CoolNetCoder22-Oct-05 4:55 
GeneralRe: Counting... Pin
gpmaker22-Oct-05 6:32
gpmaker22-Oct-05 6:32 
GeneralTerrific Library Pin
j1webb27-Sep-05 9:40
j1webb27-Sep-05 9:40 
GeneralRe: Terrific Library Pin
gpmaker27-Sep-05 12:03
gpmaker27-Sep-05 12:03 
GeneralFile Path functions Pin
Bud Pass18-Sep-05 13:39
Bud Pass18-Sep-05 13:39 
GeneralRe: File Path functions Pin
gpmaker18-Sep-05 13:41
gpmaker18-Sep-05 13:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.