Finding if certain characters exist in a string 
Author Message
 Finding if certain characters exist in a string

I have a string and i would like to be able to find out if it contains 4
points "." is this possible in vb 5? Also would it be the same for words?

Thanks

Alex



Wed, 18 Jun 1902 08:00:00 GMT  
 Finding if certain characters exist in a string
I don't think there is a VB method to do this, I wrote my own

Public Function countString(ByVal stringToSearchThrough As String, _
    ByVal stringToCount As String, Optional ByVal _
    compareMethod As VbCompareMethod = vbTextCompare) As Integer
    ' returns how often a string appears in another one
    Dim positionFound As Long, quantityFound As Long
    Do
        positionFound = InStr(positionFound + 1, _
            stringToSearchThrough, stringToCount, _
            compareMethod)
        If positionFound >= 1 Then
            quantityFound = quantityFound + 1
        Else
            Exit Do
        End If
    Loop
    countString = quantityFound
End Function

Now you would write "if countString(myString, ".") = 4 then..."

--
http://www.hitnet-ev.de/~lenssen



Quote:
> I have a string and i would like to be able to find out if it contains 4
> points "." is this possible in vb 5? Also would it be the same for words?

> Thanks

> Alex



Wed, 18 Jun 1902 08:00:00 GMT  
 Finding if certain characters exist in a string
i am not sure if you want to look for
"XXXX.XXXX.XXXX.XXXX.XXXX" - four "."
or
"XXXXXXXXXXX....XXXXXXXXX" - four "."'s

to do the first
strX = "XXXX.XXXX.XXXX.XXXX.XXXX"
Dim r as Variant
r = split(strX, ".")
(r is now a 1 dimensional string array, so count through its items (with a
for each loop) to see if there are 5.)

to do the second
strX = "XXXXXXXXXXX....XXXXXXXXX"
if InStr(strX, "....") <> 0 then
    'Do something here
end if

Quote:
> Also would it be the same for words?

yes


Quote:
> I don't think there is a VB method to do this, I wrote my own

> Public Function countString(ByVal stringToSearchThrough As String, _
>     ByVal stringToCount As String, Optional ByVal _
>     compareMethod As VbCompareMethod = vbTextCompare) As Integer
>     ' returns how often a string appears in another one
>     Dim positionFound As Long, quantityFound As Long
>     Do
>         positionFound = InStr(positionFound + 1, _
>             stringToSearchThrough, stringToCount, _
>             compareMethod)
>         If positionFound >= 1 Then
>             quantityFound = quantityFound + 1
>         Else
>             Exit Do
>         End If
>     Loop
>     countString = quantityFound
> End Function

> Now you would write "if countString(myString, ".") = 4 then..."

> --
> http://www.hitnet-ev.de/~lenssen



> > I have a string and i would like to be able to find out if it contains 4
> > points "." is this possible in vb 5? Also would it be the same for
words?

> > Thanks

> > Alex



Wed, 18 Jun 1902 08:00:00 GMT  
 Finding if certain characters exist in a string
If split works in VB5 already, then the function could be a oneliner...
actually, one could just remember the approach

    if UBound(Split(myString, ".")) = 4 then...

--
http://www.hitnet-ev.de/~lenssen


Quote:
> i am not sure if you want to look for
> "XXXX.XXXX.XXXX.XXXX.XXXX" - four "."
> or
> "XXXXXXXXXXX....XXXXXXXXX" - four "."'s

> to do the first
> strX = "XXXX.XXXX.XXXX.XXXX.XXXX"
> Dim r as Variant
> r = split(strX, ".")
> (r is now a 1 dimensional string array, so count through its items (with a
> for each loop) to see if there are 5.)

> to do the second
> strX = "XXXXXXXXXXX....XXXXXXXXX"
> if InStr(strX, "....") <> 0 then
>     'Do something here
> end if
> > Also would it be the same for words?
> yes



> > I don't think there is a VB method to do this, I wrote my own

> > Public Function countString(ByVal stringToSearchThrough As String, _
> >     ByVal stringToCount As String, Optional ByVal _
> >     compareMethod As VbCompareMethod = vbTextCompare) As Integer
> >     ' returns how often a string appears in another one
> >     Dim positionFound As Long, quantityFound As Long
> >     Do
> >         positionFound = InStr(positionFound + 1, _
> >             stringToSearchThrough, stringToCount, _
> >             compareMethod)
> >         If positionFound >= 1 Then
> >             quantityFound = quantityFound + 1
> >         Else
> >             Exit Do
> >         End If
> >     Loop
> >     countString = quantityFound
> > End Function

> > Now you would write "if countString(myString, ".") = 4 then..."

> > --
> > http://www.hitnet-ev.de/~lenssen



> > > I have a string and i would like to be able to find out if it contains
4
> > > points "." is this possible in vb 5? Also would it be the same for
> words?

> > > Thanks

> > > Alex



Wed, 18 Jun 1902 08:00:00 GMT  
 Finding if certain characters exist in a string

Quote:
> to do the first
> strX = "XXXX.XXXX.XXXX.XXXX.XXXX"
> Dim r as Variant
> r = split(strX, ".")
> (r is now a 1 dimensional string array, so count through its items (with a
> for each loop) to see if there are 5.)

why not dimension r as a dynamic string array? and instead of looping
through the array, just check the upper bound and see if it is 4 (0 to 4 =
5). the last will either be blank or whatever comes after the last "." which
gives the extra 5th.

strX = "XXXX.XXXX.XXXX.XXXX.XXXX"
Dim r() as String
r = Split(strX, ".")
If UBound(r) = 4 Then 'there are 4 points "."

or make it a function:

strX = "XXXX.XXXX.XXXX.XXXX.XXXX"

Function FindNumOfStrings(ByVal SearchString As String, ByVal SearchFor As
String, HowMany As Long) As Boolean
  Dim r() As String
  r = Split(SearchString, SearchFor)
  If UBound(r) = HowMany Then SomeFunc = True
End Function

Private Sub Command1_Click()
  If FindNumOfStrings(strX, ".", 4) Then Debug.Print "There are 4 points
""."""
End Sub

Boolean values are False by default and the function will not retain its
last value, so if it is not set to true within itself it will be False so
there is no need to add and Else statement to set the function to False.

Mr. X



Wed, 18 Jun 1902 08:00:00 GMT  
 Finding if certain characters exist in a string

Quote:
> I have a string and i would like to be able to find out if it contains 4
> points "." is this possible in vb 5? Also would it be the same for words?

in VB6 works InStr


Wed, 18 Jun 1902 08:00:00 GMT  
 Finding if certain characters exist in a string
Split() is only supported in VB6.

--
Howard Henry Schlunder
AutoSigAmp Plug-In by Howard:
 Winamp is currently playing:
 chumbawumba--the big issue wma


Quote:
> If split works in VB5 already, then the function could be a oneliner...
> actually, one could just remember the approach

>     if UBound(Split(myString, ".")) = 4 then...

> --
> http://www.hitnet-ev.de/~lenssen


> > i am not sure if you want to look for
> > "XXXX.XXXX.XXXX.XXXX.XXXX" - four "."
> > or
> > "XXXXXXXXXXX....XXXXXXXXX" - four "."'s

> > to do the first
> > strX = "XXXX.XXXX.XXXX.XXXX.XXXX"
> > Dim r as Variant
> > r = split(strX, ".")
> > (r is now a 1 dimensional string array, so count through its items (with
a
> > for each loop) to see if there are 5.)

> > to do the second
> > strX = "XXXXXXXXXXX....XXXXXXXXX"
> > if InStr(strX, "....") <> 0 then
> >     'Do something here
> > end if
> > > Also would it be the same for words?
> > yes



> > > I don't think there is a VB method to do this, I wrote my own

> > > Public Function countString(ByVal stringToSearchThrough As String, _
> > >     ByVal stringToCount As String, Optional ByVal _
> > >     compareMethod As VbCompareMethod = vbTextCompare) As Integer
> > >     ' returns how often a string appears in another one
> > >     Dim positionFound As Long, quantityFound As Long
> > >     Do
> > >         positionFound = InStr(positionFound + 1, _
> > >             stringToSearchThrough, stringToCount, _
> > >             compareMethod)
> > >         If positionFound >= 1 Then
> > >             quantityFound = quantityFound + 1
> > >         Else
> > >             Exit Do
> > >         End If
> > >     Loop
> > >     countString = quantityFound
> > > End Function

> > > Now you would write "if countString(myString, ".") = 4 then..."

> > > --
> > > http://www.hitnet-ev.de/~lenssen



> > > > I have a string and i would like to be able to find out if it
contains
> 4
> > > > points "." is this possible in vb 5? Also would it be the same for
> > words?

> > > > Thanks

> > > > Alex



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Finding & Counting Certain Characters in a string

2. Search a string and change a character if it exists in the string

3. Extracting A Certain Character From A String

4. Regexp to match a subset of strings but exclude all of a certain character

5. Checking a String for a Certain Character

6. procedure to find certain files from a certain date on harddisk

7. finding a specific character in a string

8. Find/replace characters in a string

9. finding the most common character in a string

10. Find character "-" in a string

11. How do you find Null characters in a string

12. find double byte characters in a single byte string

 

 
Powered by phpBB® Forum Software