Results 1 to 12 of 12

Thread: Proper Case In VbScript

  1. #1

    Thread Starter
    Hyperactive Member MeTTa@'s Avatar
    Join Date
    Aug 2005
    Posts
    312

    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.

  2. #2
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    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.

  3. #3
    Junior Member
    Join Date
    Feb 2004
    Location
    Hull, England
    Posts
    19

    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.

  4. #4
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Proper Case In VbScript

    You need to write your own title case or propercase function here is one that I use -

    VB Code:
    1. Function ProperCase(sText)
    2. '*** Converts text to proper case e.g.  ***'
    3. '*** surname = Surname                  ***'
    4. '*** o'connor = O'Connor                ***'
    5.  
    6.     Dim a, iLen, bSpace, tmpX, tmpFull
    7.  
    8.     iLen = Len(sText)
    9.     For a = 1 To iLen
    10.     If a <> 1 Then 'just to make sure 1st character is upper and the rest lower'
    11.         If bSpace = True Then
    12.             tmpX = UCase(mid(sText,a,1))
    13.             bSpace = False
    14.         Else
    15.         tmpX=LCase(mid(sText,a,1))
    16.             If tmpX = " " Or tmpX = "'" Then bSpace = True
    17.         End if
    18.     Else
    19.         tmpX = UCase(mid(sText,a,1))
    20.     End if
    21.     tmpFull = tmpFull & tmpX
    22.     Next
    23.     ProperCase = tmpFull
    24. End Function

  5. #5
    Lively Member
    Join Date
    Oct 2004
    Posts
    88

    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

  6. #6
    Lively Member
    Join Date
    Oct 2004
    Posts
    88

    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

  7. #7
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    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

  8. #8
    New Member
    Join Date
    Aug 2009
    Posts
    1

    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.

  9. #9
    New Member
    Join Date
    Jan 2011
    Posts
    1

    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.

  10. #10
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Proper Case In VbScript

    BaileyPhil,

    You call it like this -
    vb Code:
    1. Dim sMyLCName: sMyLCName="o'connor"
    2. Dim sMyPCName: sMyPCName=""
    3.  
    4. ' ProperCase(sText)
    5. sMyPCName=ProperCase(sMyLCName)
    6.  
    7. ' Output the result
    8. Response.Write ("Original lower name: " & sMyLCName & "<br>")
    9. Response.Write ("Proper case: " & sMyPCName & "<br>")

    Hope this helps

    Al

  11. #11
    New Member
    Join Date
    Aug 2001
    Location
    Sharjah
    Posts
    2

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

  12. #12
    Addicted Member
    Join Date
    Jul 2006
    Posts
    159

    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
  •  



Click Here to Expand Forum to Full Width