Gets the string in the closed symbol

Source: Internet
Author: User
Tags exit expression integer
String when resolving the IIF problem, in order to determine whether the IIF statement is legitimate, and find the conditions therein, return the value True and return the value false. It's not natural. A general function module is written to solve a string of words in the closed symbolic form. such as "(...)", "[...]" , reader can also set a closed symbol or string, but it must be symmetric, such as "AVB" and "Def" can be combined into a pair, so for the string avbcdeokdef in the closed symbol. These functions are sorted as follows, parameters, return values and function functions have been explained in the comments, I hope this function module can be helpful to VB loyal enthusiasts.





' --------------------------------------------------------------------------

' Look for a symbol from a section of text (consider case) and return all index of the symbol's location

' Douhapy 2005-01-31

'

Parameters

' Strsentence: Any piece of text

' Strsymbol: Special symbol to look for, or string

' Symbolindex (): Returns the index of where the symbol is located in the text

' Blcasematch: Must match case (True case must match)

' Bldesc: Symbolindex data in descending order (True to descending order index)

'

' Return value:

' True successfully found the symbol and Symbolindex has the corresponding value

' --------------------------------------------------------------------------

Function checksymbolfromsentence (ByVal strsentence As String, ByVal Strsymbol As String, _

ByRef Symbolindex () as Integer, Optional ByVal blcasematch = True, Optional ByVal bldesc = False) as Boolean



Dim Intsymbolindex () as Integer

Dim strtmp as String

Dim Inttmp as Integer

Dim Blreturn as Boolean



Dim I as Integer



strtmp = Strsentence:blreturn = false:i = 0



If Bldesc Then

If Blcasematch Then

Inttmp = InStrRev (strtmp, Strsymbol)

Else

Inttmp = InStrRev (strtmp, Strsymbol,-1, vbTextCompare)

End If

Else

If Blcasematch Then

Inttmp = InStr (strtmp, Strsymbol)

Else

Inttmp = INSTR (1, strtmp, Strsymbol, vbTextCompare)

End If

End If



Do While inttmp <> 0

Blreturn = True

ReDim Preserve Intsymbolindex (i)

Intsymbolindex (i) = Inttmp

Inttmp = intTmp-1



If inttmp <> 0 Then

If Bldesc Then

If Blcasematch Then

Inttmp = InStrRev (strtmp, Strsymbol, inttmp)

Else

Inttmp = InStrRev (strtmp, Strsymbol, inttmp, vbTextCompare)

End If

Else

If Blcasematch Then

Inttmp = InStr (inttmp + 1, strtmp, Strsymbol)

Else

Inttmp = InStr (inttmp + 1, strtmp, Strsymbol, vbTextCompare)

End If

End If

End If

i = i + 1

Loop



Checksymbolfromsentence = Blreturn

Symbolindex = Intsymbolindex



Erase Intsymbolindex

End Function



' --------------------------------------------------------------------------

' Get any piece of text ' (...) String data in a closed symbol

' Douhapy 2005-01-31

'

Parameters

' Strsentence: Any piece of text

' Leftbracketindex: Index of the left sign of the closed symbol in this paragraph of text

' Leftclosesymbol: Left symbol for closed sign

' Rightclosesymbol: The right sign of the closed symbol

' Blcasematch: Must match case (True case must match)

'

' Return value

' If successful, return the string in the closing parenthesis

' Otherwise return an empty string

' --------------------------------------------------------------------------

Function getclosestring (ByVal strsentence as String, ByVal Leftbracketindex as Integer, _

Optional ByVal Leftclosesymbol As String = "(", Optional ByVal rightclosesymbol As String = ")", _

Optional ByVal blcasematch as Boolean = True) as String



Dim Strreturn as String

Dim strtmp as String



Dim Intleftbracketindex () as Integer ' position of all left parenthesis

Dim Intrightbracketindex () as Integer ' position of all closing brackets



Dim I as Integer

Dim J as Integer

Dim m as Integer

Dim Mintleftbracketindex as Integer

Dim Mintrightbracketindex as Integer



strtmp = Strsentence:strreturn = ""

' Find the first opening parenthesis

If Blcasematch Then

Mintleftbracketindex = INSTR (1, strsentence, Leftclosesymbol)

Else

Mintleftbracketindex = INSTR (1, Strsentence, Leftclosesymbol, vbTextCompare)

End If



If mintleftbracketindex <> 0 Then

If UCase (Mid (strsentence, Leftbracketindex, Len (leftclosesymbol)) = UCase (Leftclosesymbol) Then

Mintleftbracketindex = Leftbracketindex

End If

Else

GoTo Endlab

End If



' Get all the left and right parenthesis positions

Call Checksymbolfromsentence (strtmp, Leftclosesymbol, Intleftbracketindex, Blcasematch, True)

Call Checksymbolfromsentence (strtmp, Rightclosesymbol, Intrightbracketindex, Blcasematch, True)



If UBound (Intleftbracketindex) = UBound (Intrightbracketindex) Then

' Loop to find the matching right and left symmetry bracket while putting the data to 0

For i = 0 to UBound (intleftbracketindex)



For j = 0 to UBound (intrightbracketindex)

If Intrightbracketindex (j) <> 0 Then

If Intrightbracketindex (j) < Intleftbracketindex (i) Then

Exit for

End If



If j = UBound (Intrightbracketindex) Then

j = j + 1:exit for

End If



End If

Next



For m = j-1 to 0 Step-1

If Intrightbracketindex (m) <> 0 Then

If Intleftbracketindex (i) = Mintleftbracketindex Then

Mintrightbracketindex = Intrightbracketindex (M)

End If

Intrightbracketindex (m) = 0

Exit for

End If

Next

Next



Strreturn = Mid (strtmp, Mintleftbracketindex + Len (leftclosesymbol), _

Mintrightbracketindex-mintleftbracketindex-len (Rightclosesymbol))

End If



Endlab:

getclosestring = Strreturn

Erase Intleftbracketindex

Erase Intrightbracketindex

End Function





' -------------------------------------------------------------------------------------------------------------- --------------------------------------

' Check the conditional expression, true expression, false expression in the statement placed in the corresponding ' () ' in the IIF statement

' Douhapy 2005-01-31

'

Parameters

' Strsentence: Any of the statements (the statement contains IIF)

' Strcondition: Returns the conditional expression in the IIF statement

' Strreturnt: Returns the true expression in the IIF statement

' STRRETURNF: Returns the false expression in the IIF statement

'

' Return value:

' True to find the desired expression successfully

' -------------------------------------------------------------------------------------------------------------- --------------------------------------

Function checkiifsentence (ByVal strsentence As String, Optional ByRef strcondition As String = "", _

Optional ByRef strreturnt As String = "", Optional ByRef strreturnf As String = "") as Boolean



Dim strtmp as String



Dim Striifsentence as String

Conditions in Dim mstrcondition as String ' IIF statement

True result in Dim mstrreturnt as String ' IIF statement

False result in Dim mstrreturnf as String ' IIF statement



Dim INTTMP1 as Integer

Dim INTTMP2 as Integer



Dim I as Integer

Dim J as Integer

Dim m as Integer



Dim Blsucceed as Boolean

Dim Intleftbracketindex () as Integer ' position of all left parenthesis

Dim Intrightbracketindex () as Integer ' position of all closing brackets



' --------------------------------------------------------------------------

' Find IIf first (to determine if the IIF statement

strtmp = Replace (Strsentence, "", "")

If InStr (1, Strtmp, "IIf (", vbTextCompare) = 0 Then

Exit Function

End If



' --------------------------------------------------------------------------

' Get an expression in IIf

'

strtmp = Strsentence

INTTMP1 = INSTR (1, strtmp, "IIf", vbTextCompare)

If INTTMP1 Then



' Get the position of the nearest left parenthesis from the IIF and save

INTTMP1 = InStr (INTTMP1, Strtmp, "(")



Striifsentence = getclosestring (strtmp, INTTMP1)

Blsucceed = True

End If



' --------------------------------------------------------------------------

' Get conditions in IIF and return values

If Blsucceed Then

Blsucceed = False



' Get conditions

INTTMP1 = INSTR (1, Striifsentence, ",", vbTextCompare)

If intTmp1 <> 0 Then

Mstrcondition = Mid (striifsentence, 1, inttmp1-1)



INTTMP2 = InStr (intTmp1 + 1, striifsentence, ",", vbTextCompare)

If intTmp2 <> 0 Then



' Get return value

Mstrreturnt = Mid (striifsentence, INTTMP1 + 1, inttmp2-inttmp1-1)

MSTRRETURNF = Mid (striifsentence, INTTMP2 + 1, Len (striifsentence)-INTTMP2)



Blsucceed = True

End If

End If

End If



Checkiifsentence = Blsucceed

Strcondition = Mstrcondition

Strreturnt = mstrreturnt

STRRETURNF = Mstrreturnf



End Function



Private Sub Command1_Click ()

Dim strtmp as String

Conditions in Dim strcondition as String ' IIF statement

True result in Dim strreturnt as String ' IIF statement

False result in Dim strreturnf as String ' IIF statement



strtmp = "IIf ((a+b)-(B+a))-((B-6)-c) * A, Standout,outtime)-(STANDOUT-OUTTIME/2) > (standout + outtime)"

If checkiifsentence (strtmp, Strcondition, Strreturnt, STRRETURNF) Then

MsgBox "original statement:" & vbCrLf & strtmp & VbCrLf & _

"The conditions in the IIF statement:" & strcondition & VbCrLf & VbCrLf & _

"True return value in IIF statement:" & Strreturnt & VbCrLf & _

"False return value in IIF statement:" & STRRETURNF



MsgBox getclosestring (strtmp, 57)



End If



strtmp = "{[a123befgcb[[[[]

MsgBox getclosestring (strtmp, 4, "{[[A]," [[[[], False)



End Sub



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.