break apart variable 
Author Message
 break apart variable

I have a variable which will contain one or two word seperated by a space if
there is two.
I need to check and see if there is two words and assign them to two
seperate variables if there is.

I have tried playing around with mid and len but can't quite get the code
right to acomplish this.

Any help would be appreciated...

Thanks



Tue, 06 Apr 2004 07:59:53 GMT  
 break apart variable
<%
 Dim a, b, c

 a = "Word1 Word2"

 If InStr(a, " ") = 0 Then
  b = a
 Else
  b = Left(a, InStr(a, " ") - 1)
  c = Right(a, InStrRev(a, " ") - 1)
 End If

 Response.Write "b: '" & b & "'<br>"
 Response.Write "c: '" & c & "'"
%>

If the variable a does not have a space in it, b will be set to a, otherwise
b will be set to the first word and c to the second (the space will be
removed)

--
Yours sincerely,
Jacob Munk-Stander

Microsoft MVP | Microsoft Associate Expert

Quote:

> I have a variable which will contain one or two word seperated by a space
if
> there is two.
> I need to check and see if there is two words and assign them to two
> seperate variables if there is.

> I have tried playing around with mid and len but can't quite get the code
> right to acomplish this.

> Any help would be appreciated...

> Thanks



Tue, 06 Apr 2004 08:07:23 GMT  
 break apart variable
How about using split to separate the words?  Below aryWords(0) will hold
"one" and aryWords(1) will hold "two" after the split.  You can check the
uBound to see how many words there where.

Dim aryWords, strWords
strWords = "one two"
aryWords = split(strWords, " ")
for i = 0 to uBound(aryWords)
  response.write "Word(" & i & ") = " & aryWords(i) & "...<BR>"
next

Ron

Quote:

> I have a variable which will contain one or two word seperated by a space
if
> there is two.
> I need to check and see if there is two words and assign them to two
> seperate variables if there is.

> I have tried playing around with mid and len but can't quite get the code
> right to acomplish this.

> Any help would be appreciated...

> Thanks



Tue, 06 Apr 2004 09:37:36 GMT  
 break apart variable
Thank you for your reply,

The code below detects the space if there is one and cuts the first word out
of the variable just fine, however it seems to assume that the second word
is the same length as the first and if not, it makes it the same length by
using part of the first word and the space or shortens the second word if it
is longer.

I am new to VBScript and have tried to trouble shoot this so far without
success

Thanks again


Quote:
> <%
>  Dim a, b, c

>  a = "Word1 Word2"

>  If InStr(a, " ") = 0 Then
>   b = a
>  Else
>   b = Left(a, InStr(a, " ") - 1)
>   c = Right(a, InStrRev(a, " ") - 1)
>  End If

>  Response.Write "b: '" & b & "'<br>"
>  Response.Write "c: '" & c & "'"
> %>

> If the variable a does not have a space in it, b will be set to a,
otherwise
> b will be set to the first word and c to the second (the space will be
> removed)

> --
> Yours sincerely,
> Jacob Munk-Stander

> Microsoft MVP | Microsoft Associate Expert


> > I have a variable which will contain one or two word seperated by a
space
> if
> > there is two.
> > I need to check and see if there is two words and assign them to two
> > seperate variables if there is.

> > I have tried playing around with mid and len but can't quite get the
code
> > right to acomplish this.

> > Any help would be appreciated...

> > Thanks



Wed, 07 Apr 2004 05:05:57 GMT  
 break apart variable
Thanks for your reply Ron also,

I haven't tried this code yet, but I do intend to...

Thanks again


Quote:
> How about using split to separate the words?  Below aryWords(0) will hold
> "one" and aryWords(1) will hold "two" after the split.  You can check the
> uBound to see how many words there where.

> Dim aryWords, strWords
> strWords = "one two"
> aryWords = split(strWords, " ")
> for i = 0 to uBound(aryWords)
>   response.write "Word(" & i & ") = " & aryWords(i) & "...<BR>"
> next

> Ron


> > I have a variable which will contain one or two word seperated by a
space
> if
> > there is two.
> > I need to check and see if there is two words and assign them to two
> > seperate variables if there is.

> > I have tried playing around with mid and len but can't quite get the
code
> > right to acomplish this.

> > Any help would be appreciated...

> > Thanks



Wed, 07 Apr 2004 05:15:36 GMT  
 break apart variable
Try this (I tested it and it works)

<%
  Dim a, b, c, intSplit

  a = "Word1 LongerWord2"

  intSplit = Instr(a, " ")
  If intSplit = 0 Then
   b = a
  Else
   b = Left(a, intSplit - 1)
   c = Right(a, Len(a) - intSplit)
  End If

  Response.Write "b: '" & b & "'<br>"
  Response.Write "c: '" & c & "'"
%>



Wed, 07 Apr 2004 05:14:28 GMT  
 break apart variable
Got it:

c = mid(a,InStrRev(a," ")+1)         instead of:
c = Right(a,InStrRev(a," ")-1)

I am sure it was just a typo on your part....thanks again :)



Quote:
> <%
>  Dim a, b, c

>  a = "Word1 Word2"

>  If InStr(a, " ") = 0 Then
>   b = a
>  Else
>   b = Left(a, InStr(a, " ") - 1)
>   c = Right(a, InStrRev(a, " ") - 1)
>  End If

>  Response.Write "b: '" & b & "'<br>"
>  Response.Write "c: '" & c & "'"
> %>

> If the variable a does not have a space in it, b will be set to a,
otherwise
> b will be set to the first word and c to the second (the space will be
> removed)

> --
> Yours sincerely,
> Jacob Munk-Stander

> Microsoft MVP | Microsoft Associate Expert


> > I have a variable which will contain one or two word seperated by a
space
> if
> > there is two.
> > I need to check and see if there is two words and assign them to two
> > seperate variables if there is.

> > I have tried playing around with mid and len but can't quite get the
code
> > right to acomplish this.

> > Any help would be appreciated...

> > Thanks



Wed, 07 Apr 2004 05:45:12 GMT  
 break apart variable
Wow,

You guys are great!

Thanks for all the code, I intend to study it all for future reference

Thanks again!!


Quote:
> Try this (I tested it and it works)

> <%
>   Dim a, b, c, intSplit

>   a = "Word1 LongerWord2"

>   intSplit = Instr(a, " ")
>   If intSplit = 0 Then
>    b = a
>   Else
>    b = Left(a, intSplit - 1)
>    c = Right(a, Len(a) - intSplit)
>   End If

>   Response.Write "b: '" & b & "'<br>"
>   Response.Write "c: '" & c & "'"
> %>



Wed, 07 Apr 2004 05:47:11 GMT  
 break apart variable
Damn... I'm sorry about that, it tends to get a bit late for me here in
Denmark when replying to these newsgroups... (it's 4.05AM in the morning
here - been studying CS all night long... no life... probably should have
gone to town instead...)

Anyway, that's besides the point :o) Here's another version that doesn't
care about the size of the two words:

<%
    Dim a, b, c

    a = "Word EvenLongerWord"

    If InStr(a, " ") = 0 Then
        b = a
    Else
        b = Left(a, InStr(a, " ") - 1)
        c = Right(a, Len(a) - InStrRev(a, " "))
    End If

    Response.Write "b: '" & b & "'<br>"
    Response.Write "c: '" & c & "'"
%>

Have a good weekend!

--
Yours sincerely,
Jacob Munk-Stander

Microsoft MVP | Microsoft Associate Expert

Quote:

> Thank you for your reply,

> The code below detects the space if there is one and cuts the first word
out
> of the variable just fine, however it seems to assume that the second word
> is the same length as the first and if not, it makes it the same length by
> using part of the first word and the space or shortens the second word if
it
> is longer.

> I am new to vbscript and have tried to trouble shoot this so far without
> success

> Thanks again


> > <%
> >  Dim a, b, c

> >  a = "Word1 Word2"

> >  If InStr(a, " ") = 0 Then
> >   b = a
> >  Else
> >   b = Left(a, InStr(a, " ") - 1)
> >   c = Right(a, InStrRev(a, " ") - 1)
> >  End If

> >  Response.Write "b: '" & b & "'<br>"
> >  Response.Write "c: '" & c & "'"
> > %>

> > If the variable a does not have a space in it, b will be set to a,
> otherwise
> > b will be set to the first word and c to the second (the space will be
> > removed)

> > --
> > Yours sincerely,
> > Jacob Munk-Stander

> > Microsoft MVP | Microsoft Associate Expert




- Show quoted text -

Quote:
> > > I have a variable which will contain one or two word seperated by a
> space
> > if
> > > there is two.
> > > I need to check and see if there is two words and assign them to two
> > > seperate variables if there is.

> > > I have tried playing around with mid and len but can't quite get the
> code
> > > right to acomplish this.

> > > Any help would be appreciated...

> > > Thanks



Wed, 07 Apr 2004 10:07:52 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Break Apart String Using Index

2. breaking apart a string

3. Break Apart String Using Index

4. Break Apart String Using Index

5. Break apart (split) a string in Visual Basic/Access 97

6. Help : Breaking One WORD variable to two BYTE variable in VB

7. Breaking a date into variables

8. Break value of variables

9. XP broke part of VB6 IDE - hovering over a variable while executing no longer works

10. Shared variable not updating after page break

11. shared variable fails on soft page break

12. HELP!!!! Taking apart City-State-Zip field problem

 

 
Powered by phpBB® Forum Software