devxlogo

Create a Safer Mid Function

Create a Safer Mid Function

If you often write complex string-parsing and manipulation algorithms, the last thing you want is to add more checks to ensure your string positions are not negative. Avoid the hassle by using this function when you need to use Mid. It wraps around native VB functionality and handles this common error case:

 Public Function FlexiMid(From As String, ByVal Start _	As Long, Optional Length As Long = -1) As String	If Start < 1 Then Start = 1	If Length = -1 Then ' they want the rest of it		FlexiMid = Mid$(From, Start)	Else ' just give what they want		FlexiMid = Mid$(From, Start, Length))	End IfEnd Function

Once you paste this function into your program?I recommend a module, so you can access it from anywhere?you can use it as you would Mid. In fact, once I wrote this, I ran a search-and-replace on my project to start using it throughout.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist