Click here to Skip to main content
Click here to Skip to main content

String Manipulation Library

, 27 Sep 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
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

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'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)

Share

About the Author

gpmaker

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 PinmemberChintanhiremath21-Mar-12 19:34 
GeneralC# version of these functions Pinmembergpmaker12-Apr-08 19:46 
GeneralRe: C# version of these functions PinmemberVCKicks16-Dec-08 9:58 
Questionhow to get numbers from text.. PinmemberEng.hamzeh Abualrob25-Nov-07 0:16 
AnswerRe: how to get numbers from text.. PinmemberVCKicks31-Mar-08 19:27 
Generalthank you Pinmemberriquard10-Nov-07 15:28 
Generalthnx again.. PinmemberBeBadgujar17-Jun-07 19:30 
Questionthank you very much..!!!but..... PinmemberBeBadgujar10-Jun-07 19:33 
AnswerRe: thank you very much..!!!but..... Pinmembergpmaker10-Jun-07 19:40 
Questionhow to extrat the name of the folder? PinmemberBeBadgujar8-Jun-07 23:58 
AnswerRe: how to extrat the name of the folder? Pinmembergpmaker10-Jun-07 11:43 
GeneralAnother Needed Function: Name Case PinmemberShutlOrbit26-Apr-07 21:04 
QuestionSuite - or is it Sweet ?? Pinmemberstixoffire18-Mar-07 5:45 
GeneralVB6 Shell function PinmemberFIFI512-Mar-07 8:56 
GeneralGood job! PinmemberGarret1177-Jul-06 4:28 
GeneralGreat Library PinmemberooKmonkey5-May-06 1:49 
Questionany tip for parsing? Pinmemberkenkenkennek23-Apr-06 1:26 
GeneralGood Work Pinmemberxoxoxoxoxoxox11-Jan-06 13:29 
GeneralCounting... PinmemberCoolNetCoder22-Oct-05 5:55 
GeneralRe: Counting... Pinmembergpmaker22-Oct-05 7:32 
GeneralTerrific Library Pinmemberj1webb27-Sep-05 10:40 
GeneralRe: Terrific Library Pinmembergpmaker27-Sep-05 13:03 
GeneralFile Path functions PinmemberBud Pass18-Sep-05 14:39 
GeneralRe: File Path functions Pinmembergpmaker18-Sep-05 14:41 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 27 Sep 2005
Article Copyright 2005 by gpmaker
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid