Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Obtain a subset of a string? 1

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
US
Hi,

Does anyone know a way to copy a portion of a string based on a set of positions?

Consider the following string:

theString = "10.11.130.80 - firstname lastname [12/Mar/2001:10:37:53 +0600]"

If I split this string based on spaces, then my array looks like the following:

Array

10.11.130.80
-
firstname
lastname
[12/Mar/2001:10:37:53
+0600]

The problem with this is that I'd to keep the 'firstname' and 'lastname' together, so I can assign
the entire name to a variable. Ideally, I just want everything between the - and [.

I'm able to identify the starting position of 'firstname' and the starting position of the
'['. I'd like to simply read everything between positions A and B. The only problem is that I
can't read this string into any kind of array.

Is there a way to read each individual character into an array? That way I can just reference
the characters I want.

Thanks in advance for the help.
scripter73


 
Code:
Dim instr   as long
Dim lngBeg  as long
Dim strIP   as string
Dim strName as string
strIP = ""
strName = ""
lngInstr = Instr(1,strW," - ")
if lngInstr = 0 then LngInstr = Len(strW) + 1
strIP = Left(strW,lngInstr - 1)
lngBeg = lngInstr + 3
if lngBeg <= Len(strW) then  ' avoid minus length
    lngInstr = Instr(lngBeg,strW,&quot; [&quot;)
    if lngInstr = 0 then LngInstr = Len(strW) + 1
    strName = Mid$(strW,lngBeg,lngInstr - LngBeg)
end if
There is also InstrRev.
And, yes MID$ (return string) is faster than, and is not the same as MID (return variant string) although MS does not document it. I've tested it and Mid$, Left$, Right$ etc produces faster code.
 
Thanks so much John! This really helped out!

scripter73
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top