Jump to content

Converting Strings To Title Case


Recommended Posts

Try this:

$foo = "this is some sentence"

MsgBox(4096,"", _StringInitial($foo))
Exit


Func _StringInitial($text)
   Local $i, $temp, $char, $isFirstLetter = 1
   
   For $i = 1 to StringLen($text)
      $char =  StringMid($text, $i, 1)
      If $isFirstLetter And Asc($char) >= 97 And Asc($char) <= 122 Then
         $tmp = $tmp & Chr(Asc($char)-32)
         $isFirstLetter = 0
      ElseIf $char = " " Then
         $isFirstLetter = 1
         $tmp = $tmp & " "
      Else
         $tmp = $tmp & $char
      EndIf
   Next
   Return $tmp
EndFunc

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

Do you mean every worse should start with a capital letter, like this:

This Is A Test Sentence.

If so, that's simple. StringSplit using space as a delimiter, If the first character of each elment is an alpha character, Run that through the upper case function and re-build the string.

Link to comment
Share on other sites

My version of Valik's thought:

$x="If so, that's simple. StringSplit using space as a delimiter, If the first character of each elment is an alpha character, Run that through the upper case function and re-build the string."
MsgBox(1,"",Title($x))



Func Title($_x)
$_y=""
$_title=StringSplit($_x," ")
For $i=1 To $_title[0]
$_y=$_y & " "& StringUpper ( StringLeft($_title[$i],1) ) & StringTrimLeft($_title[$i],1)
Next
Return StringTrimLeft($_y,1)
EndFunc

No need to determine if first letter is a letter or not, StringUpper will only campitalise letters.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

does the 'stringsplit' method correctly capitalize "word in quotes"?

My code should, though you might want to make the following change to the ElseIf-clause:

ElseIf $char = " " or $char = @TAB or $char = @LF or $char = @CR Then
Edited by CyberSlug
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

Ok here is one to play around with, copy what ever you like into the clipboard, and run this:

It will capitalise pretty well for ya

$x=clipget()
msgbox(1,"",Titlecase($x))

Func Titlecase($_x)
$y=StringLeft($_x,1)
$y1=$y
$out=StringUpper($y)
$_x=StringTrimLeft ($_x,1)

for $i=0 to Stringlen($_x)
$y=StringLeft($_x,1)
if asc($y1)>90 and asc($y1)<97 or(asc($y1)<65 or asc($y1)>122) then
$out=$out&StringUpper($y)
else
$out=$out&$y
endif
$y1=StringLeft($_x,1)
$_x=StringTrimLeft ($_x,1)
next
return $out
EndFunc
Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

Oops, I Forgot You Want Title Case, Not Only Capitalise The First Letter, So Here Ya Go:

$x=clipget()

msgbox(1,"",Titlecase($x))

Func Titlecase($_x)
$y=StringLeft($_x,1)
$y1=$y
$out=StringUpper($y)
$_x=StringTrimLeft ($_x,1)

for $i=0 to Stringlen($_x)
$y=StringLeft($_x,1)
if asc($y1)>90 and asc($y1)<97 or(asc($y1)<65 or asc($y1)>122) then
$out=$out&StringUpper($y)
else
$out=$out&StringLower($y)
endif
$y1=StringLeft($_x,1)
$_x=StringTrimLeft ($_x,1)
next
return $out
EndFunc

example

This could be234tough|to work[out]title_case,so you may#wish to adjust it?

becomes

This Could Be234Tough|To Work[Out]Title_Case,So You May#Wish To Adjust It?

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...