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

Taking values from a string 2

Status
Not open for further replies.

guitardave78

Programmer
Sep 5, 2001
1,294
0
0
GB
Hey there guys and gals.

Sting manipulatio, my worst subject

I have a string that looks like this

"-username blah -password blahblah -root c:\blah"

I need to get those values into

dim username as string, password as string, root as string
username = "blah"
password = "blahblah "
root = "c:\blah"

i would normally split it by splitting on the "-" then splitting each bit by " ", but there may be spaces int the actuall strings so i can just do a simple split :(

Cheers

}...the bane of my life!
 
Ok this is how I worked it out, is there a better way?

Code:
params = split("-username blah -password blahblah -root c:\blah","-")
For n = 1 To UBound(params)
    param = (Left(params(n), InStr(params(n), " ")))
    pValue = (Mid(params(n), InStr(params(n), " "), Len(params(n))))
    Select Case Trim(param)
        Case "username"
            username = pValue
        Case "password"
            password = pValue
        Case "root"
            root= pValue
    End Select
Next

}...the bane of my life!
 
Is that working for the username?

By default, an array (like params) will have a lower bound of 0. Of course, you can change that if you want to.

If you do...

Dim Params() As string
params = split("-username blah -password blahblah -root c:\blah","-")

Then...
Params(0) = "username blah"
Params(1) = "password blahblah"
Params(2) = "root C:\blah"

Your loop is defined as...
For n = 1 to UBound(params)

Which would really go from 1 to 2, getting the password and root.

You can fix this in a couple different ways. In my opinion, the best way to solve this problem would be to change the loop.

For n = LBound(Params) to UBound(Params)

And
Root appears to be a folder. Folders can have dashes (-) in their names. This would give you an incorrect value for the root value.

For example...
"-username blah -password blahblah -root c:\blah[red]-blah[/red]"

Using your algorithm, the value for root would be c:\blah instead of c:\blah-blah



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Regular expressions will do it. Give them a try and post back here if you have problems. As a starting point here is vbscript to doa what you want:

(I prototype in VBScript a lot because I think it is faster to come up with a solution then translate it to VB/VBA as needed)

Code:
strTest = "-username blah -password blahblah -root c:\blah"

Set oRE = New Regexp

oRE.Pattern = "^-*username (.*) -*password (.*) -*root (.*)$"

Set cMatches = oRE.Execute(strTest)
For Each oMatch In cMatches
	WScript.Echo oMatch.SubMatches(0) & vbTab & oMatch.SubMatches(1) & vbTab & oMatch.SubMatches(2)
Next

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I now realize that the first part of my comment was unnecessary because your first character is a dash (which I hadn't noticed). So, your array would have 4 elements (0 to 3) the Zero'th element would always be an empty string.

Generally speaking, using dashes to seperate items in a string is going to be problematic for you. In your case, you can probably have usernames with dashes, passwords with dashes, and folders with dashes. It may never happen, but you never know what a user will do.

Instead, I would suggest that you use a vbCRLF instead. As far as I know, you cannot have a username, password, or folder with a CR-LF in them.

If you cannot change how the string is stored and/or retrieved, then the following method would probably be your next best choice.

Code:
Dim sTemp As String
Dim arTemp() As String
Dim n As Long

sTemp = "-username My-User -password blah-password -root c:\blah-Blah"
sTemp = Replace(sTemp, "-username ", vbCrLf & "username ", 1, 1, vbTextCompare)
sTemp = Replace(sTemp, "-password ", vbCrLf & "password ", 1, 1, vbTextCompare)
sTemp = Replace(sTemp, "-root ", vbCrLf & "root ", 1, 1, vbTextCompare)

arTemp = Split(sTemp, vbCrLf)
For n = LBound(arTemp) To UBound(arTemp)
    Debug.Print arTemp(n)
Next

Unfortunately, this isn't perfect either. If you start with...
sTemp = "-username My-User -password my-root -root c:\blah-Blah"

You'll see that the password and root are not correct.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I tested George's last test string with the regex pattern I specified ("-username My-User -password my-root -root c:\blah-Blah") and it worked for that as well.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Having made sure that the entire string begins with "-" you could probably split by " -", as long as the values for username and path don't contatin " -" in them.
-Max
 
I'm with Tom go with RegExp.


Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
I promise! I'm going to learn RegExp someday.

I took me a while to test your code. I needed to add a reference to "Microsoft VBScript Regular Expressions 5.5".

Of course, you're right. It works perfectly.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
>I'm going to learn RegExp

Slowly, slowly they all turn to the dark side ...
 
>> Slowly, slowly

Yep. Look at this, thread222-1099235, especially near the bottom where I make the same claim. [bigsmile]


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
there may no always be all of the parameters passed, so i cant use the regex you demonstrate (i think)

From a user point i want to use - for the switch, but for now i have just switched over to ? which they can use in a file or folder :)

}...the bane of my life!
 
In that case:
Code:
strTest = "-password blahblah -root c:\blah"

strUserPattern = "-*username (\S*)"
strPassPattern = "-*password (\S*)"
strRootPattern = "-*root (\S*)"

Set oRE = New Regexp

oRE.Pattern = strUserPattern
If oRE.Test(strTest) Then
	Set cMatch = oRE.Execute(strTest)
	strUser = cMatch.Item(0).SubMatches(0)
End If
oRE.Pattern = strPassPattern
If oRE.Test(strTest) Then
	Set cMatch = oRE.Execute(strTest)
	strPass = cMatch.Item(0).SubMatches(0)
End If
oRE.Pattern = strRootPattern
If oRE.Test(strTest) Then
	Set cMatch = oRE.Execute(strTest)
	strRoot = cMatch.Item(0).SubMatches(0)
End If

WScript.Echo "User = " & strUser
WScript.Echo "Pass = " & strPass
WScript.Echo "Root = " & strRoot

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top