Alternate ways to extract a substring?

R

Robert Crandal

I have the following simple code:

sTxt = Mid(sSentence, 14, 5)

This code copies 5 characters from the string contained
in the variable "sSentence", starting at position 14.

My question is, is there another way to do the same
exact thing WITHOUT using the "Mid()" function???

I'm basically trying to find other ways to extract substrings
without using the MID() function.

Thank you!

Robert Crandal
 
R

Ron Rosenfeld

I have the following simple code:

sTxt = Mid(sSentence, 14, 5)

This code copies 5 characters from the string contained
in the variable "sSentence", starting at position 14.

My question is, is there another way to do the same
exact thing WITHOUT using the "Mid()" function???

I'm basically trying to find other ways to extract substrings
without using the MID() function.

Thank you!

Robert Crandal

Here are several methods

=====================
Option Explicit
Sub foo()
Const S As String = "Now is the time for all"
Debug.Print Mid(S, 14, 5)

'Use combination of Left and Right
Debug.Print Left(Right(S, Len(S) - (14 - 1)), 5)

'Using nested worksheet function REPLACE
With WorksheetFunction
Debug.Print .Replace(.Replace([A1], 1, 13, ""), 5 + 1, 99, "")
End With

'Using regular Expressions
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Pattern = "^[\s\S]{13}([\s\S]{5})[\s\S]*"
Debug.Print re.Replace(S, "$1")

Debug.Print vbLf

End Sub
==========================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top