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
Printable View
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
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.
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.
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
Here's the simplier solution:
mystring="dan"
firstLetter=UCase(Left(mystring,1))
otherLetters=Right(mystring,Len(mystring)-1)
mystring=firstLetter & otherLetters
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
Imbrod,
Your functions won't handle names like o'connor and hammer-smith etc. but maybe those won't be necessary in Metta@ scenairo
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
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
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
Dim upper as String = "converted from lowercase"
Console.WriteLine(upper.ToUpper())
Console.WriteLine(UCase(upper))
Console.WriteLine(StrConv(upper,VbStrConv.UpperCase))
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