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

parsing through string, looking for coma

Status
Not open for further replies.

striker73

MIS
Jun 7, 2001
376
US
I am passing the value "citystate" into a form. This value is in the format such as "Tustin, CA". I want to be able to parse through the string and pull out two values: city and state. If I limit the user to only two characters for state, I think I'm okay with using the right(string,length) syntax, but how can I pull the rest of the string? I know some languages have a substr() function or something similar. Is there any way to pull out all of a string until the coma? Any ideas? Thanks!
 
you could just get rid of the comma like this.
<HTML>
<HEAD><TITLE></TITLE>
<SCRIPT LANGUAGE=&quot;VBScript&quot;>
<!--
Sub extract()
Dim TheForm, MyVar
TheForm=form1.Text1.value
'Start by trimming leading/trailing spaces

'Now, replace spaces...
Do While InStr(1, TheForm, &quot;,&quot;)
TheForm = Replace(TheForm, &quot;,&quot;, &quot;&quot;)
Loop
MyVar = MsgBox (&quot;Here it is without commas &quot; & TheForm)
End Sub

-->
</SCRIPT>
</HEAD>
<BODY>
<H3>Simple Validation</H3><HR>
<form Name=&quot;form1&quot;>

<input TYPE=&quot;TEXT&quot; name=&quot;Text1&quot; SIZE=&quot;30&quot;>
<input TYPE=&quot;button&quot; VALUE=&quot;reformat&quot; onClick=&quot;extract()&quot;>
</form>
</BODY>
</HTML>

I may not get it the 1st or 2nd time,
but how sweet that 15th time can be.
 
I read your question as you want to split the value on the commma to retrieve the city and state.

The easiest way would be to split the value into an array:
CityState = &quot;Dallas, TX&quot;
tmpArray = Split(CityState,&quot;,&quot;)
City = tmpArray(0)
State = Trim(tmpArray(1))
 
how do i delcare the array? I have tried:

Dim TempArray()
Redim TempArray(2)

But I always get an error message (script out of range) when I try to do the following:

temparray = split(citystate,&quot;,&quot;)
city = temparray(0)
state = trim(temparray(1)) [/color.
 
how do i delcare the array? I have tried:

Dim TempArray()
Redim TempArray(2)

But I always get an error message (script out of range) when I try to do the following:

temparray = split(citystate,&quot;,&quot;)
city = temparray(0)
state = trim(temparray(1))
 
Suppose one of your entries is &quot;&quot;. Split returns an array with UBound = -1 (Yes, -1).
Try this in case '&quot; or no comma producing UBound=0
Dim tempArray
temparray = split(citystate,&quot;,&quot;)
' Never trust outside data. Bulletproof
Redim Preserve tempArray(1) ' Guarantee 2 entries
city = temparray(0)
state = trim(temparray(1)) Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top