Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General 2 / February 2004



Tip: Looking for answers? Try searching our database.

string search

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sheoran - 11 Feb 2004 10:11 GMT
I want to parse my string in a loop from right to left(unlike instr
which is L to R) to look for a literal in a string .
Is there any inbuilt function like InStr in VB to acheive this.

Thanks in advance,
Jiten
Dag Sunde - 11 Feb 2004 11:05 GMT
> I want to parse my string in a loop from right to left(unlike instr
> which is L to R) to look for a literal in a string .
> Is there any inbuilt function like InStr in VB to acheive this.

InStrRev(...) is available in VB6... Don't know about VB5.

--
Dag.
J French - 11 Feb 2004 11:20 GMT
>> I want to parse my string in a loop from right to left(unlike instr
>> which is L to R) to look for a literal in a string .
>> Is there any inbuilt function like InStr in VB to acheive this.
>
>InStrRev(...) is available in VB6... Don't know about VB5.

VB5

' ###########################################
'
' Returns Reverse Search Position or Len( Target$ ) + 1
' If Start = 0 then Starts at End
'
' Searches BACK from Start - if not found then returns 0
'
' Fix: Q <= Start 24/10/02 JF
Public Function InstrRev&(ByVal Start&, Target$, SubStr$)

  Dim Q&, LastQ&

  If Start = 0 Then _
     Start = Len(Target$) + 1

  Q = 1
  While Q > 0
        Q = InStr(Q, Target$, SubStr$)
        If Q > 0 Then
           If Q <= Start Then 'JF
              LastQ = Q
              Q = Q + 1
           End If
           If Q > Start Then _
              Q = 0
        End If
  Wend
 
  InstrRev = LastQ
End Function
Geoff - 11 Feb 2004 18:13 GMT
> >> I want to parse my string in a loop from right to left(unlike instr
> >> which is L to R) to look for a literal in a string .
[quoted text clipped - 34 lines]
>    InstrRev = LastQ
> End Function

This could be made compatable with VB6 only needs a small change
any new code or sample code would then run under either

Public Function InstrRev&(Target$, SubStr$, Optional ByVal Start& = -1)
   Dim Q&, LastQ&
   If Start < 1 Or Start > Len(Target$) Then _
       Start = Len(Target$)
   Q = 1
   While Q > 0
       Q = InStr(Q, Target$, SubStr$)
<Snip>
Just an idea
Geoff
--
WStoreyII - 29 Feb 2004 07:32 GMT
> >> I want to parse my string in a loop from right to left(unlike instr
> >> which is L to R) to look for a literal in a string .
[quoted text clipped - 34 lines]
>    InstrRev = LastQ
> End Function
i am kind of new to this and dont understand how this works
how would i use this to look for a letter in a string like for instance
mystring = instr(thestring, " ") would find any spaces what character can i
insert that will return the first letter that is found in the string?
Randy Day - 29 Feb 2004 16:19 GMT
[snip]

> i am kind of new to this and dont understand how this works
> how would i use this to look for a letter in a string like for instance
> mystring = instr(thestring, " ") would find any spaces what character can i
> insert that will return the first letter that is found in the string?

There is no predefined function to do what you want.
Here's the VB6 way to do what you're asking:

private function GetCharPos(strSearch as string, bSearchMode as byte) as
integer
dim bFlag as boolean
dim Position as integer
dim LenStr as integer

bFlag=false

'if bSearchMode=1 : detect the first letter position
'if bSearchMode=2 : detect the first number position
'if bSearchMode=3 : detect the first other char. position

LenStr=len(strSearch)  
Position=0

do while not bFlag and Position <= LenStr

    Position=Position+1

    select case mid$(strSearch,Position,1)
    case 'a' to 'z','A' to 'Z':
        if bSearchMode=1 then bflag=true
    case '0' to '9':
        if bSearchMode=2 then bflag=true
    case else:
        if bSearchMode=3 then bflag=true
    end select
loop

'if Position is greater than the length of the string
'there are no chars. of the requested type, so return 0
if Position > LenStr then Position = 0

GetCharPos=Position
end Function
Geoff - 29 Feb 2004 19:14 GMT
> [snip]
>
[quoted text clipped - 17 lines]
> 'if bSearchMode=2 : detect the first number position
> 'if bSearchMode=3 : detect the first other char. position
<SNIP>

You could use the Like operator to add flexibility it
if it appeals
e.g

Option Explicit
Const strMyString = "Whats this VB6! - or 5 ? "
Private Sub Command1_Click()
   Debug.Print "First Ucase Character Pos = "; GetFirstPos(strMyString,
"[A-Z]")
   Debug.Print "First Lcase Character Pos = "; GetFirstPos(strMyString,
"[a-z]")
   Debug.Print "First Character Pos = "; GetFirstPos(strMyString, "[A-Z
,a-z]")
   Debug.Print "First Number Pos = "; GetFirstPos(strMyString, "#")
   Debug.Print "First Exclamation = "; GetFirstPos(strMyString, "!")
   Debug.Print "First Question Mark Pos = "; GetFirstPos(strMyString,
"[?]")
   Debug.Print "First space Pos = "; GetFirstPos(strMyString, " ")
End Sub
Public Function GetFirstPos(strfSearch As String, strLikeSearch As String)
As Integer
   Dim Position As Integer
   Dim LenStr As Integer
   LenStr = Len(strfSearch)
   Position = 1
   Do Until Position > LenStr
       If Mid$(strfSearch, Position, 1) Like strLikeSearch Then Exit Do
       Position = Position + 1
   Loop
   'if Position is greater than the length of the string
   'there are no chars. of the requested type, so return 0
   If Position > LenStr Then Position = 0
   GetFirstPos = Position
End Function
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.