# Thread: Exercise in string function simplicity (Word 97SR2)

1. ## 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?)

2. ## 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. ## Re: Exercise in string function simplicity (Word 97SR2)

You're weird: shouldn't the function have some calling parameters?

4. ## 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. ## Re: Exercise in string function simplicity (Word 9

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

Andrew

6. ## 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.

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

Not a bad idea, my friend.

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

Oh yes, oh yes. Calling parameters. What was I thinking....

9. ## 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. ## 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?

11. ## 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...

12. ## 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

<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...

13. ## 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...

14. ## 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.

15. ## 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 Last

#### Posting Permissions

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