Finding if certain characters exist in a string
Author |
Message |
Alex #1 / 7
|
 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 |
|
 |
Philipp Lensse #2 / 7
|
 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 |
|
 |
dsavits #3 / 7
|
 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 |
|
 |
Philipp Lensse #4 / 7
|
 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 |
|
 |
Mr. #5 / 7
|
 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 |
|
 |
KOOM #6 / 7
|
 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 |
|
 |
Howard Henry Schlunde #7 / 7
|
 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 |
|
|
|