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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing a string

Status
Not open for further replies.
Feb 8, 2005
6
US
I need to parse thru a string one charactor at a time until it it meets a condition and move it into a memory holder and continue until the string lenght is null. AN example would do nicely.

Thanks
Craig
 
Take a look at the For .. Next instruction and at the Len & Mid functions.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I guess I need to be a little clearer. I need to be able to step thru a string one character at a time and move it into a place holder. I guess I am asking for a function that already does that.

Example:
Dim getAlpha as string
Dim getNumeric as string
Dim statement as string

Statement = “ my address is 1234 main st.”

The place holders would look something like this;

GetAlpha = my address is
GetNumeric = 1234

How would I do that?

Craig
 
Please follow PHV's suggestion and look in Help. Look up all the string functions. You will have to BUILD the function you want, using whatever combination of Left, Right, Len, Mid, InStr, Trim etc etc - whatever string functions you need.

In your example:

“ my address is 1234 main st.”

you say you want GetAlpha to = "my address is". Fine, what about "main st"? What about that space before "my".

You have to determine EXACTLY what you want, and use the functions available (listed above). We are not here to post full answers for people. We certainly do post direct answers to specific problems, but your request is a general one. It involves basic understanding of string manipulation. This is required knowledge if you want to use VBA. Look up the examples in Help. What you appear to want to do can be done.

For example, is the originating string always going to be the same length? That will have to be a consideration. Look up Len.

At PHV suggests, look up For...Next to walk through each character.

If you have specific and real problems with the functions, post your code and we will can point out where you may have gone off track, or suggest easier ways of doing something.


Gerry
 
Ok I can move thru sort of, but how do I check to see if the character is alpha or numeric?

Sub main()
Dim reg As String
Dim myChar As String
Dim i As Integer
Dim strText As String
Dim j As Integer

strText = "Lost in space"
i = Len(strText)
If Len(strText) > 0 Then
Do Until i = 0

reg = Right(strText, i)
strText = reg
i = i - 1
Debug.Print strText
Debug.Print reg
Loop

End If


End Sub

is there a better way?
Craig
 
If IsNumeric(reg)
If reg Like "[A-Z]"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
ok, just had time to rework work the problem and it works. Thanks for your help. now however, it has cropped up a new one. When I parse thru the string the alpha chars go thru ok but when it hits the numeric this go wrong. The routine truncates the values for some reason. Can some one help?

Sub Try()
Dim h As Integer
Dim i As Integer

Dim m As String
Dim s As String
Dim t As String

Dim str As String
Dim myStr As String


'Deny inbound UDP from 207.130.91.139/2809 to 207.131.220.17/1434 on interface ahmcorp
s = "from 207.130.91.139/2809"


For i = 0 To Len(s)
m = Left(s, 1)
t = m
s = Replace(s, m, "")
Do
If t Like "[a-z]" Then 'stripping the alpha char
myStr = myStr + t
ElseIf IsNumeric(t) Then 'stripping the numeric char
str = str + t
Debug.Print str 'Watching the numeric value change

ElseIf t Like "[.]" Then 'looking for the period to add to the numeric value
str = str + t
End If

Loop Until h = 0

Next i

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top