Results 1 to 3 of 3
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Code: Function strOnly(strIn As String, strRef As

    Create a new module (in VBE choose Insert, Module) and name it "Strng" (View, Properties)

    Into this module paste the following code:

    <pre>Public Function strOnly(strIn As String, strRef As String) As String
    ' Procedure : strOnly
    ' Description: This code returns only those characters in strIn which can be found in strRef.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: A data string, a reference string.
    ' Returns: STRING.
    ' Assumes: Nothing
    ' Side Effects: None.
    ' Tested: By the calls shown below.
    Dim strOut As String
    strOut = ""
    Dim lngI As Long
    lngI = 1
    For lngI = 1 To Len(strIn)
    If InStr(1, strRef, Mid(strIn, lngI, 1)) > 0 Then
    strOut = strOut & Mid(strIn, lngI, 1)
    Else
    End If
    Next lngI
    strOnly = strOut
    'Sub TESTstrOnly()
    'MsgBox strOnly("alphabet", "abcde") ' regular use
    'MsgBox strOnly("", "abcde") ' empty source string
    'MsgBox strOnly("alphabet", "") ' empty reference string
    'MsgBox strOnly("alphabetand1digit", "abcdefghijklmnopqrstuvwxyz") ' strip digits
    'End Sub
    End Function
    </pre>



    To test the code, or to single-step through it, select and drag the commented lines that start with "'Sub TESTstrOnly()" to a point just beyond the "End Function" and de-comment them.

    Place the text cursor anywhere in the decommented TEST procedure and tap the <F5> function key.


    strOnly will be used in the next posts to build more useful functions, which will be used to build more useful functions such as strBreakFileString, which will be used in the INI functions strGP and strPP.

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strOnly(strIn As String, strRef As

    Chris,

    Is there a reason to initialise lngI when it's being initialised in the next line?

    I agree with initialising strOUT- although it's not really necessary as it is initialised to an empty string anyway, it's nice when stepping through code to be able to drag the start point back to a point where everyting gets reset if necessary.

    Should "Mid(strIn, lngI, 1)" be set to a string, as the same expression is evaluated twice?
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strOnly(strIn As String, strRef As

    > Is there a reason to initialise lngI when it's being


    No. You are correct; that should be removed. I think it is detrius from those earlier submissions (remember I was using While/Wend loops and you guys talked me into For/Next loops?

    > the start point back to a point where everyting gets reset


    Exactly! Also required in some mainframe langauges; i think of it as "a good programming practice"

    > Should "Mid(strIn, lngI, 1)" be set to

    Exactly.

    I will implement these corrections in my code.

    If I ever get to post the template containing the INI suite of functions (my first batch here) it ought to have all corrected code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •