string manipulation
Author Message
string manipulation

hi,
i am trying to manipulate a string.  I have in my form module
dim string1 as string.
this string will hold numbers separated by spaces. ie. "3 46 54 324 5 34"
now i need to do 2 things.
I would need to search this string for a value.  Let's say i am looking for
"3", then it should be able to find it.  but if I am looking for 6, I don't
want it to find it becuase the number in the string is 46, not 6.  is there a
way to do that.
secondly, if I want to take out the number 46 from the string, how would I do
that?

rafael chemtob

Tue, 11 May 2004 13:10:26 GMT
string manipulation

Quote:
>hi,
>i am trying to manipulate a string.  I have in my form module
>dim string1 as string.
>this string will hold numbers separated by spaces. ie. "3 46 54 324 5 34"
>now i need to do 2 things.
>I would need to search this string for a value.  Let's say i am looking for
>"3", then it should be able to find it.  but if I am looking for 6, I don't
>want it to find it becuase the number in the string is 46, not 6.  is there a
>way to do that.

As you said the numbers are separated with spaces, so you could then
Dim s As String
s = " " & number & " "  ' which equals " 6 " if number is 6.. duh. :)
ret = InStr(buffer, s)  ' will return the offset where the first match
is found in the buffer.

Quote:
>secondly, if I want to take out the number 46 from the string, how would I do
>that?

Once you have found the offset, use the Left() and Mid() functions to
cut the string. ie :
s = "3 46 54 324 5 34"
offset = 12 'cut 5 out
s1 = left(s,offset) & mid(s, offset + len(" 5"))
s1 then would = "3 46 54 324 34" I think. :)
Play with it and you'll catch on.

Regards, Frank

Tue, 11 May 2004 13:45:13 GMT
string manipulation
RChemtobb:

Howdy.  Simply add a space to the beginning and end of the string.  This
gaurantees that every number is bracketed by spaces.  Now, when you want to find
6, but not 46 as per your example, just use INSTR to search for " 6 "  <---
that's a six with a space on both sides.  Finally, if you want to remove a number
from the original string, INSTR tells you where that number is found within the
string so you can use the following formula to remove whatever substring you have
found:

sData = " 3 46 54 324 5 34 "
sFind = " 6 "
iPos = instr(sData, sFind)
if iPos then
' sFind was found within sData,
' to remove sFind from sData:
sData = left\$(sData, iPos + 1) & mid\$(sData, iPos + len(sFind))
end if

chris judge

Quote:

> hi,
> i am trying to manipulate a string.  I have in my form module
> dim string1 as string.
> this string will hold numbers separated by spaces. ie. "3 46 54 324 5 34"
> now i need to do 2 things.
> I would need to search this string for a value.  Let's say i am looking for
> "3", then it should be able to find it.  but if I am looking for 6, I don't
> want it to find it becuase the number in the string is 46, not 6.  is there a
> way to do that.
> secondly, if I want to take out the number 46 from the string, how would I do
> that?

> rafael chemtob

Tue, 11 May 2004 13:51:56 GMT
string manipulation
On Fri, 23 Nov 2001 05:51:56 GMT, Chris Judge

Quote:

>sData = " 3 46 54 324 5 34 "
>sFind = " 6 "
>iPos = instr(sData, sFind)
>if iPos then
>    ' sFind was found within sData,
>    ' to remove sFind from sData:
>    sData = left\$(sData, iPos + 1) & mid\$(sData, iPos + len(sFind))
>end if

Actually, in this case it gets a little trickier than that, as
removing both spaces will break the original format of space
delimiting.. Or do we let him sweat on that one  ? :-)

Regards, Frank

Tue, 11 May 2004 14:48:30 GMT
string manipulation

Quote:
> As you said the numbers are separated with spaces, so you could then
> make your search like this.
> Dim s As String
> s = " " & number & " "  ' which equals " 6 " if number is 6.. duh. :)
> ret = InStr(buffer, s)  ' will return the offset where the first match
> is found in the buffer.

The only addition I would make to this is to do your InStr like as follows

ret = InStr(" " & buffer & " ", s)

so that the first and last items can be found.

Rick

______________________________________________________________________________
Posted Via Binaries.net = SPEED+RETENTION+COMPLETION = http://www.binaries.net

Wed, 12 May 2004 01:17:53 GMT
string manipulation
Have you played about with the Split() function?, VB6.0 ...
This takes a delimited string and splits it into array elements
e.g.

Dim TheArray() as String
Dim TheString as String

TheString = "3 46 54 324 5 34"

TheArray = Split(TheString," ")        'delimited by a space

This takes the string and creates an array with in this case 6 elements, you
are then free
to what ever you want with the array, e.g. search for a particular value.

Incidently the Join() function does the opposite of the Split() function

Hope this helps

Paul

Quote:
> hi,
> i am trying to manipulate a string.  I have in my form module
> dim string1 as string.
> this string will hold numbers separated by spaces. ie. "3 46 54 324 5 34"
> now i need to do 2 things.
> I would need to search this string for a value.  Let's say i am looking
for
> "3", then it should be able to find it.  but if I am looking for 6, I
don't
> want it to find it becuase the number in the string is 46, not 6.  is
there a
> way to do that.
> secondly, if I want to take out the number 46 from the string, how would I
do
> that?