Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For Each loop concatenate results 2

Status
Not open for further replies.

woter324

Technical User
Jan 26, 2007
179
GB
Hi all,

I am sure this is an easy one, but I think the seasonal effects of Muld Wine are getting to me.

I am trying to join / concatenate the output of the for loop.

Here is my code:
Code:
wscript.Echo sCase("john smith went shopping")

Function sCase(strSentence)
aSentence = Split(strSentence," ")
For Each word In aSentence
	intWord = Len(word)
	strFirstLetter = UCase(Left(word, 1))
	strRemainingLetters = LCase(Right(word, intWord - 1))
	strWord = strFirstLetter & strRemainingLetters
Next
strWord1 = strWord & " " & strWord
sCase = strword1
End Function

I want it to return "John Smith Went Shopping" but it only returns "Shopping Shopping"

I am sure I have done this several time before, but I just can't work it out at the moment :-(.

Many thanks

Tex
 
The line
Code:
strWord = strFirstLetter & strRemainingLetters
is getting over written in each iteration of the loop. You will need to do something like
Code:
sCase = sCase & strFirstLetter & strRemainingLetters & " "
Also the lines
Code:
strWord1 = strWord & " " & strWord
sCase strword1
are not needed but you could use
Code:
sCase = RTrim(sCase)
to get rid of the trailing " " (space)


If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
What about this ?
Code:
Function sCase(strSentence)
For Each word In Split(strSentence)
  strWord = strWord & " " & UCase(Left(word, 1)) & LCase(Mid(word, 2))
Next
sCase = Mid(strWord, 2)
End Function

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Foada,

Thanks very much. I was close, but working on the wrong line. I could see it was overwriting but not at that point. Doh.

Here's what I now have:
Code:
Function sCase(strSentence)
aSentence = Split(strSentence," ")
For Each word In aSentence
	intWord = Len(word)
	strFirstLetter = UCase(Left(word, 1))
	strRemainingLetters = LCase(Right(word, intWord - 1))
	sCase = sCase & strFirstLetter & strRemainingLetters & " "
Next
End Function

Thanks again.
 
PHV,

So simple. Thank you very much.

Thanks

Tex
 
Glad to help.

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top