By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,294 Members | 2,484 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,294 IT Pros & Developers. It's quick & easy.

Need a simple logic for a simple text-manipulation procedure

P: n/a
Rex
Hi I want to write a procedure which takes in a string of names
seperated by a whitespace and puts commas at each whitespace the last
name however, should have "and" before it.

Let me explain that with the help of an example:

The original string is: "Toby Grant Michelle Tom" the procedure should
return "Toby, Grant, Michelle and Tom"

Cheers!
Rex

Nov 21 '06 #1
Share this Question
Share on Google+
23 Replies


P: n/a
On 20 Nov 2006 17:12:57 -0800, "Rex" <ra*******@gmail.comwrote:

Actually, the proper punctuation is:
"Toby, Grant, Michelle, and Tom"
ref:
http://dictionary.reference.com/writ...nctuation.html

Use a combination of these functions: Replace, InStr, Left$, Mid$,
Right$.

-Tom.

>Hi I want to write a procedure which takes in a string of names
seperated by a whitespace and puts commas at each whitespace the last
name however, should have "and" before it.

Let me explain that with the help of an example:

The original string is: "Toby Grant Michelle Tom" the procedure should
return "Toby, Grant, Michelle and Tom"

Cheers!
Rex
Nov 21 '06 #2

P: n/a

Tom van Stiphout wrote:
On 20 Nov 2006 17:12:57 -0800, "Rex" <ra*******@gmail.comwrote:

Actually, the proper punctuation is:
"Toby, Grant, Michelle, and Tom"
ref:
http://dictionary.reference.com/writ...nctuation.html

Use a combination of these functions: Replace, InStr, Left$, Mid$,
Right$.
or use split and join

dim elements() as string
data = "Toby Grant Michelle Tom"
data = replace( data, " ", " " )
elements = split( data, " " )
elements( ubound(elements) ) = "and " & elements(ubound(elements))
data = trim(join( elements, ", "))

Cheers :-)

Keith

Nov 21 '06 #3

P: n/a
Tom van Stiphout wrote:
On 20 Nov 2006 17:12:57 -0800, "Rex" <ra*******@gmail.comwrote:

Actually, the proper punctuation is:
"Toby, Grant, Michelle, and Tom"
ref:
http://dictionary.reference.com/writ...nctuation.html
Some would argue that the last comma is redundant, that "and" is
entirely sufficient to separate the second-last element of the series
from the last and that the inclusion of the comma is inefficient. I am
one of those.

So the Oxford Language gurus say otherwise? Too bad.
Nov 21 '06 #4

P: n/a
That depends on the context and whether the comma disambiguates the
phrase.

"property management, engineering services and technical supervision"
would be fine.
"property management, tools and supplies, technical supervision and
maintenance" may not be if you meant "property management, tools and
supplies, technical supervision, and maintenance".

Lyle Fairfield wrote:
Tom van Stiphout wrote:
On 20 Nov 2006 17:12:57 -0800, "Rex" <ra*******@gmail.comwrote:

Actually, the proper punctuation is:
"Toby, Grant, Michelle, and Tom"
ref:
http://dictionary.reference.com/writ...nctuation.html

Some would argue that the last comma is redundant, that "and" is
entirely sufficient to separate the second-last element of the series
from the last and that the inclusion of the comma is inefficient. I am
one of those.

So the Oxford Language gurus say otherwise? Too bad.
Nov 21 '06 #5

P: n/a
li******@gmail.com wrote:
Tom van Stiphout wrote:
>On 20 Nov 2006 17:12:57 -0800, "Rex" <ra*******@gmail.comwrote:

Actually, the proper punctuation is:
"Toby, Grant, Michelle, and Tom"
ref:
http://dictionary.reference.com/writ...nctuation.html

Use a combination of these functions: Replace, InStr, Left$, Mid$,
Right$.
or use split and join

dim elements() as string
data = "Toby Grant Michelle Tom"
data = replace( data, " ", " " )
elements = split( data, " " )
elements( ubound(elements) ) = "and " & elements(ubound(elements))
data = trim(join( elements, ", "))

Cheers :-)

Keith
StrReverse(Replace(StrReverse(Replace("Toby Grant Michelle Tom", " ", ",
")), ",", "deoxyribonucleic acid ", , 1))
Nov 21 '06 #6

P: n/a
Punctuation rules, particularly that one, were undergoing change during the
very years when you and I were learning them. The "older" way was to
precede the "and" with a comma; the newer way was to omit the comma before
the "and." In fact, I believe both ways are considered acceptable now
(unless you encounter a die-hard from the old school).

Larry Linson

"Lyle Fairfield" <ly***********@aim.comwrote in message
news:0U*****************@read1.cgocable.net...
Tom van Stiphout wrote:
>On 20 Nov 2006 17:12:57 -0800, "Rex" <ra*******@gmail.comwrote:

Actually, the proper punctuation is:
"Toby, Grant, Michelle, and Tom"
ref:
http://dictionary.reference.com/writ...nctuation.html

Some would argue that the last comma is redundant, that "and" is entirely
sufficient to separate the second-last element of the series from the last
and that the inclusion of the comma is inefficient. I am one of those.

So the Oxford Language gurus say otherwise? Too bad.

Nov 21 '06 #7

P: n/a
li******@gmail.com wrote:

: or use split and join

: dim elements() as string
: data = "Toby Grant Michelle Tom"
: data = replace( data, " ", " " )
: elements = split( data, " " )
: elements( ubound(elements) ) = "and " & elements(ubound(elements))
: data = trim(join( elements, ", "))

This works only if the string has more than 1 element.
I also prefer Lyle's style of omitting the final comma, so
I would modify the code above:

If Ubound(elements) 0 Then
elements( ubound(elements) ) = "and " & elements(ubound(elements))
If Ubound(elements) = 1 Then
data = trim(join( elements, " and "))
Else
data = trim(join( elements, ", "))
End If
End If
--thelma

: Cheers :-)

: Keith

Nov 21 '06 #8

P: n/a
"Larry Linson" <bo*****@localhost.notwrote in message
news:UOw8h.2713$w37.1078@trnddc08...
Punctuation rules, particularly that one, were undergoing change during the
very years when you and I were learning them. The "older" way was to precede
the "and" with a comma; the newer way was to omit the comma before the "and."
In fact, I believe both ways are considered acceptable now (unless you
encounter a die-hard from the old school).
If I were speaking a list like that I would pause just as much before the "and"
as I would between any of the other names. If there's a pause I use a comma.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
Nov 21 '06 #9

P: n/a
Rick Brandt wrote:
If I were speaking a list like that I would pause just as much before the "and"
as I would between any of the other names. If there's a pause I use a comma.
Professional, hockey, players, do, the, same.
Nov 21 '06 #10

P: n/a

Lyle Fairfield wrote:
Rick Brandt wrote:
If I were speaking a list like that I would pause just as much before the "and"
as I would between any of the other names. If there's a pause I use a comma.

Professional, hockey, players, do, the, same.
Suggest you all read "Eats shoots and leaves" by Lynn Truss (try
putting a comma after "Eats" and see what happens)

Nov 21 '06 #11

P: n/a
"Geoff G" <ge***@sunny-side.wswrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
>
Lyle Fairfield wrote:
>Rick Brandt wrote:
If I were speaking a list like that I would pause just as much before the
"and"
as I would between any of the other names. If there's a pause I use a
comma.

Professional, hockey, players, do, the, same.

Suggest you all read "Eats shoots and leaves" by Lynn Truss (try
putting a comma after "Eats" and see what happens)
Consider the following two phrases...

The colors used in this picture are red, blue, green, black, and white.

He still owns a black and white television.

Will the words "black and white" come out of your mouth with the same timing?
My position is that the vast majority of people will insert a pause after the
word black in the first phrase, but not in the second. If the written word is
to model speech then it seems to me that a comma belongs there. Whether that is
what the "rules" require or not is entirely beside the point.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
Nov 21 '06 #12

P: n/a
The way things roll along on the internet these days, I'm not sure the
spoken language isn't made to model the written. Many folks spend more
time typing or emailing or blogging than they do actually speaking.
Funny how that's worked out.

Nov 21 '06 #13

P: n/a
Tom van Stiphout <no*************@cox.netwrote in
news:nt********************************@4ax.com:
Actually, the proper punctuation is:
"Toby, Grant, Michelle, and Tom"
ref:
http://dictionary.reference.com/writ...nctuation.html
That depends on which style guide you use. There are two sets of
rules for a series list, the one you cite, and the one that does not
put the comma before the "and". I was taught to use the latter.
Perhaps it's one of those differences between American and British
English.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
Nov 21 '06 #14

P: n/a
"Rick Brandt" <ri*********@hotmail.comwrote in
news:7C******************@newssvr27.news.prodigy.n et:
Consider the following two phrases...

The colors used in this picture are red, blue, green, black, and
white.

He still owns a black and white television.
That's a compound adjective and ought to be written "black-and-white
television."

Next question?

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
Nov 21 '06 #15

P: n/a
"David W. Fenton" <XX*******@dfenton.com.invalidwrote in message
news:Xn**********************************@127.0.0. 1...
"Rick Brandt" <ri*********@hotmail.comwrote in
news:7C******************@newssvr27.news.prodigy.n et:
>Consider the following two phrases...

The colors used in this picture are red, blue, green, black, and
white.

He still owns a black and white television.

That's a compound adjective and ought to be written "black-and-white
television."

Next question?
Okay, substitute a new second phrase...

The dog was black and white.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
Nov 21 '06 #16

P: n/a
"David W. Fenton" wrote
That depends on which style guide you use. There are two sets of
rules for a series list, the one you cite, and the one that does not
put the comma before the "and". I was taught to use the latter.
Perhaps it's one of those differences between American and British
English.
Nah, David, it's just that you are a young whippersnapper who came along a
little late for the "proper" rules to apply. <GRINNIN', DUCKIN', & RUNNIN'>
Nov 22 '06 #17

P: n/a

"Larry Linson" <bo*****@localhost.notschreef in bericht news:YPM8h.5516$oP6.1587@trnddc03...
"David W. Fenton" wrote
That depends on which style guide you use. There are two sets of
rules for a series list, the one you cite, and the one that does not
put the comma before the "and". I was taught to use the latter.
Perhaps it's one of those differences between American and British
English.
Nah, David, it's just that you are a young whippersnapper who came along a
little late for the "proper" rules to apply. <GRINNIN', DUCKIN', & RUNNIN'
Hmmm, this is an interesting discussion on a techncal forum.
What the hell are we arguing about here??

I guess that some of us are just too old this...
Some are taught otherwise indeed ...

Arno R
Nov 22 '06 #18

P: n/a
Rex
Hi,

The logic you suggested is working but its not omitting the last comma.
Below is the actual procedure:

'============================================
Function insertCommas(data As String) As String
Dim elements() As String

elements = Split(data, " ")
Dim lastIndex As Integer
lastIndex = UBound(elements)

If lastIndex 1 Then
elements(lastIndex - 1) = " and " + elements(lastIndex - 1)
If lastIndex = 2 Then
data = Trim(Join(elements))
ElseIf lastIndex 2 Then
data = Trim(Join(elements, ", "))
End If
End If
insertCommas = data
End Function
'==============================================

Thanks
Thelma Lubkin wrote:
li******@gmail.com wrote:

: or use split and join

: dim elements() as string
: data = "Toby Grant Michelle Tom"
: data = replace( data, " ", " " )
: elements = split( data, " " )
: elements( ubound(elements) ) = "and " & elements(ubound(elements))
: data = trim(join( elements, ", "))

This works only if the string has more than 1 element.
I also prefer Lyle's style of omitting the final comma, so
I would modify the code above:

If Ubound(elements) 0 Then
elements( ubound(elements) ) = "and " & elements(ubound(elements))
If Ubound(elements) = 1 Then
data = trim(join( elements, " and "))
Else
data = trim(join( elements, ", "))
End If
End If
--thelma

: Cheers :-)

: Keith
Nov 22 '06 #19

P: n/a
Lyle Fairfield <ly***********@aim.comwrote in
news:uO******************@read2.cgocable.net:
Rick Brandt wrote:
>If I were speaking a list like that I would pause just as
much before the "and" as I would between any of the other
names. If there's a pause I use a comma.

Professional, hockey, players, do, the, same.
so, do, some, Canadian, actors!
--
Bob Quintal

PA is y I've altered my email address.

--
Posted via a free Usenet account from http://www.teranews.com

Nov 22 '06 #20

P: n/a
Rex <ra*******@gmail.comwrote:
: Hi,

: The logic you suggested is working but its not omitting the last comma.
: Below is the actual procedure:

: '============================================
: Function insertCommas(data As String) As String
: Dim elements() As String

: elements = Split(data, " ")
: Dim lastIndex As Integer
: lastIndex = UBound(elements)

: If lastIndex 1 Then
: elements(lastIndex - 1) = " and " + elements(lastIndex - 1)
: If lastIndex = 2 Then
: data = Trim(Join(elements))
: ElseIf lastIndex 2 Then
: data = Trim(Join(elements, ", "))

data = Replace(data, ", and", " and")

I know...it's ugly, but it's less ugly than
anything else that I can think of right now

I see that you fixed a few glitches I had put in.

--thelma
: End If
: End If
: insertCommas = data
: End Function
: '==============================================

: Thanks
Nov 22 '06 #21

P: n/a

Rick Brandt wrote:
"David W. Fenton" <XX*******@dfenton.com.invalidwrote in message
news:Xn**********************************@127.0.0. 1...
"Rick Brandt" <ri*********@hotmail.comwrote in
news:7C******************@newssvr27.news.prodigy.n et:

Okay, substitute a new second phrase...

The dog was black and white.

--
Is that to say, the dog was not in technicolor?

Nov 22 '06 #22

P: n/a
"Rick Brandt" <ri*********@hotmail.comwrote in
news:w0*******************@newssvr12.news.prodigy. com:
"David W. Fenton" <XX*******@dfenton.com.invalidwrote in message
news:Xn**********************************@127.0.0. 1...
>"Rick Brandt" <ri*********@hotmail.comwrote in
news:7C******************@newssvr27.news.prodigy. net:
>>Consider the following two phrases...

The colors used in this picture are red, blue, green, black, and
white.

He still owns a black and white television.

That's a compound adjective and ought to be written
"black-and-white television."

Next question?

Okay, substitute a new second phrase...

The dog was black and white.
Uh, it's a completely different grammatical structure, so different
rules apply.

Or would expect something different?

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
Nov 22 '06 #23

P: n/a
"Arno R" <ar***********@tiscali.nlwrote
Hmmm, this is an interesting discussion on a techncal forum.
What the hell are we arguing about here??
Arguing? I thought we were having a little fun with a thread that had
drifted 'way off-topic.
I guess that some of us are just too old this...
Some are taught otherwise indeed ...
Eh?

Larry


Nov 25 '06 #24

This discussion thread is closed

Replies have been disabled for this discussion.