-
Feb 8th, 2006, 10:56 AM
#1
Thread Starter
Hyperactive Member
Proper Case In VbScript
How do I format a name properly like "dan" --> "Dan" in vb script. It was something like txtFirstName.Text = StrConv(txtFirstName, vbProperCase)Cant get it to work, thanks,
___
Dan
Last edited by MeTTa@; Feb 8th, 2006 at 11:00 AM.
-
Feb 8th, 2006, 06:23 PM
#2
Frenzied Member
Re: Proper Case In VbScript
Metta@:
I don't think VB Script has a StrConv function.
The only conversion functions I am aware of are UCase(string) and LCase(string), but these convert the whole string and not just the first letter.
I don't know if this helps or not.
-
Feb 8th, 2006, 06:35 PM
#3
Junior Member
Re: Proper Case In VbScript
Like AIS4U says, there's no one-stop-shop in VBScript. You could take a strMask and an array of Null characters then make passes against strMask and strUnformated to transform this array back into strFormated.
-
Feb 9th, 2006, 02:50 PM
#4
Fanatic Member
Re: Proper Case In VbScript
You need to write your own title case or propercase function here is one that I use -
VB Code:
Function ProperCase(sText)
'*** Converts text to proper case e.g. ***'
'*** surname = Surname ***'
'*** o'connor = O'Connor ***'
Dim a, iLen, bSpace, tmpX, tmpFull
iLen = Len(sText)
For a = 1 To iLen
If a <> 1 Then 'just to make sure 1st character is upper and the rest lower'
If bSpace = True Then
tmpX = UCase(mid(sText,a,1))
bSpace = False
Else
tmpX=LCase(mid(sText,a,1))
If tmpX = " " Or tmpX = "'" Then bSpace = True
End if
Else
tmpX = UCase(mid(sText,a,1))
End if
tmpFull = tmpFull & tmpX
Next
ProperCase = tmpFull
End Function
-
Feb 11th, 2006, 07:33 AM
#5
Lively Member
Re: Proper Case In VbScript
Here's the simplier solution:
mystring="dan"
firstLetter=UCase(Left(mystring,1))
otherLetters=Right(mystring,Len(mystring)-1)
mystring=firstLetter & otherLetters
-
Feb 11th, 2006, 07:42 AM
#6
Lively Member
Re: Proper Case In VbScript
Or better yet (just to be sure remaining letters are lowercase)
mystring="dan"
firstLetter=UCase(Left(mystring,1))
otherLetters=LCase(Right(mystring,Len(mystring)-1))
mystring=firstLetter & otherLetters
-
Feb 11th, 2006, 12:07 PM
#7
Fanatic Member
Re: Proper Case In VbScript
Imbrod,
Your functions won't handle names like o'connor and hammer-smith etc. but maybe those won't be necessary in Metta@ scenairo
-
Aug 30th, 2009, 09:41 PM
#8
New Member
Re: Proper Case In VbScript
Hi,
I just joined this site to post my version of a Title Case Function. This function can handle names with characters like "John Bloggs-Smith O'Bloggery" and something like "Don't you wish Everyone's a winner"
Feel free to slice and dice this script for your own purposes.
Function TCase(strTextString)
'Convert string to Title Case
Dim arrTextItem, strTextNew
strSplitText = " '-"
For y = 1 to len(strSplitText)
strSplitItem = Mid(strSplitText,y,1)
arrTextItem = Split(strTextString, strSplitItem)
For x = 0 to Ubound(arrTextItem)
If strSplitItem = "'" Then
If Mid(arrTextItem(x),2,1) = " " Then
strTextNew = strTextNew & strSplitItem & LCase(Left(arrTextItem(x),1)) & Right(arrTextItem(x),Len(arrTextItem(x))-1)
Else
strTextNew = strTextNew & strSplitItem & UCase(Left(arrTextItem(x),1)) & Right(arrTextItem(x),Len(arrTextItem(x))-1)
End If
Else
If strSplitItem = "-" Then
strTextNew = strTextNew & strSplitItem & UCase(Left(arrTextItem(x),1)) & Right(arrTextItem(x),Len(arrTextItem(x))-1)
Else
strTextNew = strTextNew & strSplitItem & UCase(Left(arrTextItem(x),1)) & LCase(Right(arrTextItem(x),Len(arrTextItem(x))-1))
End If
End If
Next
strTextString = Right(strTextNew,Len(strTextNew)-1)
strTextNew = ""
Next
TCase = strTextString
End Function
Last edited by tangal; Aug 30th, 2009 at 11:04 PM.
Reason: Updated the script.
-
Jan 25th, 2011, 03:52 PM
#9
New Member
Re: Proper Case In VbScript
Evening all
Was wondering if somebody could be of assistance, i have read this post with interest and am just a little stuck, if anyone could explain the above?
I'm totally new to scripting so any help or guidance would be very gratefully received.
Many thanks
Last edited by baileyphil; Jan 25th, 2011 at 07:03 PM.
-
Jan 26th, 2011, 03:44 AM
#10
Fanatic Member
Re: Proper Case In VbScript
BaileyPhil,
You call it like this -
vb Code:
Dim sMyLCName: sMyLCName="o'connor"
Dim sMyPCName: sMyPCName=""
' ProperCase(sText)
sMyPCName=ProperCase(sMyLCName)
' Output the result
Response.Write ("Original lower name: " & sMyLCName & "<br>")
Response.Write ("Proper case: " & sMyPCName & "<br>")
Hope this helps
Al
-
Jul 27th, 2016, 06:50 AM
#11
New Member
Re: Proper Case In VbScript
Dim upper as String = "converted from lowercase"
Console.WriteLine(upper.ToUpper())
Console.WriteLine(UCase(upper))
Console.WriteLine(StrConv(upper,VbStrConv.UpperCase))
-
Dec 12th, 2019, 02:52 PM
#12
Addicted Member
Re: Proper Case In VbScript
Code:
Function MyPC$(Inn$)
Dim xx&, Tr$, Txt$, Strt&, Tr2$, Bad$, Tr3$
'--------------------
Txt = Inn
Txt = StrConv(Txt, vbProperCase)
'---------------O'Neal Etc------------
Strt = 1
Do
xx = InStr(Strt, Txt, "'")
If xx = 0 Then Exit Do
Strt = xx + 1
If xx < Len(Txt) And xx > 1 Then
If xx = 2 Then
Tr3 = Mid$(Txt, xx + 1, 1)
If Tr3 <> " " Then
Tr = Mid$(Txt, xx + 1, 1)
Mid$(Txt, xx + 1, 1) = UCase$(Tr)
End If
Else
Tr2 = Mid$(Txt, xx - 2, 1)
Tr3 = Mid$(Txt, xx + 1, 1)
If Tr2 = " " And Tr3 <> " " Then
Tr = Mid$(Txt, xx + 1, 1)
Mid$(Txt, xx + 1, 1) = UCase$(Tr)
End If
End If
End If
Loop
'--------Hyphens---------
Strt = 1
Do
xx = InStr(Strt, Txt, "-")
If xx = 0 Then Exit Do
Strt = xx + 1
If xx < Len(Txt) Then
Tr = Mid$(Txt, xx + 1, 1)
Mid$(Txt, xx + 1, 1) = UCase$(Tr)
End If
Loop
'-------------------------Mc-----------
Strt = 1
Do
xx = InStr(Strt, Txt, "Mc")
If xx = 0 Then Exit Do
Strt = xx + 2
If xx + 1 < Len(Txt) Then
Tr = Mid$(Txt, xx + 2, 1)
Mid$(Txt, xx + 2, 1) = UCase$(Tr)
End If
Loop
'-------------------------Mac-----------
Strt = 1
Bad = ":he:hi:hr:hz:ho:hs:ro:ru:ra:ac:ad:aq:aw:ar:ab:ki:kl:ks:"
Bad = Bad + "in:ul:um:er:es:ed:ke:ca:co:le:on:"
Do
xx = InStr(Strt, Txt, "Mac")
If xx = 0 Then Exit Do
Strt = xx + 3
If xx + 6 < Len(Txt) Then
Tr2 = Mid$(Txt, xx + 3, 2)
If InStr(Tr2, " ") Then GoTo Skip
Tr2 = ":" + Tr2 + ":"
If InStr(Bad, Tr2) Then GoTo Skip
Tr = Mid$(Txt, xx + 3, 1)
Mid$(Txt, xx + 3, 1) = UCase$(Tr)
End If
Skip:
Loop
'-----------------
MyPC = Txt
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|