Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Change the case of a string (five options)


  • Please log in to reply
2 replies to this topic
None
  • Members
  • 3199 posts
  • Last active: Nov 05 2015 09:55 PM
  • Joined: 28 Nov 2009
After posting here String Lower and Upper in a function I thought what else can I add. After some effort I added inverse the current case and Sentence case to the default UPPER, lower and Title.
ChangeCase(String,Type) { ;type is S,I,U,L, or T
If (Type="S") { ;Sentence case.
 X = I,AHK,AutoHotkey ;comma seperated list of words that should always be capitalized
 S := RegExReplace(RegExReplace(String, "(.*)", "$L{1}"), "(?<=[\.\!\?]\s|\n).|^.", "$U{0}")
 Loop Parse, X, `, ;Parse the exceptions
  S := RegExReplace(S,"i)\b" A_LoopField "\b", A_LoopField)
 Return S
}
If (Type="I") ;iNVERSE
 Return % RegExReplace(String, "([A-Z])|([a-z])", "$L1$U2")
Return % RegExReplace(String, "(.*)", "$" Type "{1}")
}

Data =
(
Ask For Help
ask Questions aND (hopEfully) Get ansWers. post helpful Tips and Tricks.
WHY? shout! tESt.
)

MsgBox % ChangeCase(Data,"S")
MsgBox % ChangeCase("This is a Test","I")
MsgBox % ChangeCase("This is a Test","U")
MsgBox % ChangeCase("This is a Test","L")
MsgBox % ChangeCase("This is a Test","T")
Edit: Fixed examples
Edit2: Improved (shortened :) ) Invert case

garry
  • Spam Officer
  • 3219 posts
  • Last active: Feb 06 2016 09:32 PM
  • Joined: 19 Apr 2005
thank you None

zloidooraque
  • Members
  • 7 posts
  • Last active: Jan 01 2016 12:54 PM
  • Joined: 20 Aug 2015
X = I,AHK,AutoHotkey,SO,Dr,Mr,Ms..etc..,Ganja,Music;comma seperated list of words that should always be capitalize

actually found this script after being frustrated, for the 1000th time i have to sentencecase there. but after i opened my ahk script to add it, spotted Clipboard := RegExReplace(Clipboard, "(((^|([.!?]+\s+))[a-z])| i | i')", "$u1") is already there lol

 

but yours is better, exceptions is nice. is it possible to add "if first two letters is like somthing, CApitalize them"? being a programmer for more than 15 years, i just hate those regexes and trying to avoid them at all costs lol. struggle with them so hard when it's needed and when actually manage to get what i want, forgetting everything in 2 days.