Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exercise in string function simplicity (Word 97SR2)

    I want a function that returns all the characters of a string up to and excluding a string within the string.

    For example,

    strName = Gimme(strName, ".")

    returns John F

    if strName = John F. Johnston

    I want the absolutely shortest piece of code codeable.
    To win this contest, you must code your function so that
    it contains fewer characters than this:

    Function Gimme() as string
    Gimme = mid$(strName,1 instr(strName,".") -1)
    end function

    (am i weird or what?)
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  2. #2
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Reading/Swindon, Berkshire, United Kingdom
    Posts
    664
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Exercise in string function simplicity (Word 97SR2)

    I presume you meant to post this on the puzzles forum and just forgot where you were?

  3. #3
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Exercise in string function simplicity (Word 97SR2)

    You're weird: shouldn't the function have some calling parameters?
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

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

    Re: Exercise in string function simplicity (Word 9

    <pre>Function G(s, d)
    G = Mid(s, 1, InStr(1, s, d) - 1)
    End Function


    Sub TESTG()
    MsgBox G("John F. Johnston", ".")
    MsgBox G("Christopher Paul Reynolds Greaves", "Paul")
    End Sub
    </pre>


    How'm I doing? (heh heh!)


    My source code is shorter than yours.

    Did you want a shorter token string too?

  5. #5
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Exercise in string function simplicity (Word 9

    How about

    G = Left(s, InStr(s, d) - 1) ?

    Andrew

  6. #6
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Exercise in string function simplicity (Word 9

    Got you beat by a mile ( OK, 7 characters)
    <pre>Function G(s, d)
    v = Split(s, d)
    G = v(0)
    End Function</pre>

    but, if my students write this, they'll get an F: it's terrible code.
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  7. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Exercise in string function simplicity (Word 97SR2)

    Not a bad idea, my friend.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Exercise in string function simplicity (Word 97SR2)

    Oh yes, oh yes. Calling parameters. What was I thinking....
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  9. #9
    Star Lounger
    Join Date
    Jan 2001
    Posts
    71
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Exercise in string function simplicity (Word 9

    And this one knocks another 5 chars off. Down to 18 now:

    Function G(s, d)
    G = Split(s, d)(0)
    End Function

  10. #10
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Exercise in string function simplicity (Word 9

    <img src=/S/ouch.gif border=0 alt=ouch width=15 height=15> <img src=/S/exclamation.gif border=0 alt=exclamation width=15 height=15>Very nice! I wondered if you could do that, but never tried. I think you win the prize. <img src=/S/bravo.gif border=0 alt=bravo width=16 height=30>

    Did you have a better solution, Kevin?
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  11. #11
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Exercise in string function simplicity (Word 9

    I didn't have any solution. I was hoping you brains would come up with a winner.

    I like Mr. Ceraso's. Unfortunately, the Split function is absent in VBA5.

    Not that that was any requirement...
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  12. #12
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Exercise in string function simplicity (Word 9

    I'll have to test this on my OXP box. Very interesting. I was hoping someone would
    come up with a solution that did not use instr. And that you've done.

    Explain how it works....like, what is that (0) doing?


    Oh, Sammy says I need to pay up as it looks like you win. So here's
    your cash

    <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15>
    <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15>
    <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15> <img src=/S/money.gif border=0 alt=money width=17 height=15>

    Don't spend it all in one post...
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  13. #13
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Exercise in string function simplicity (Word 9

    Well, yeah, that certainly cuts things down, and I thank you for
    posting. Nice to hear from you again.

    But read the new posts below...
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  14. #14
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Exercise in string function simplicity (Word 9

    That's better, but I want to see something without the instr.


    See Don's post below. Very interesting.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  15. #15
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Exercise in string function simplicity (Word 9

    If you are to use the Split function, there is no need to wrap it up in another function

    Split("John F. Johnston",". ")(0) returns John F

    and

    Split("John F. Johnston",". ")(1) returns Johnston.

    Split returns an array, zero based. To prevent the second element (Johnston) having a leading space in this instance, the delimiter is a period + space (. )

    Andrew

Page 1 of 2 12 LastLast

Posting Permissions

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