Powershell,77个 75字节
该解决方案完全符合任务规则(甚至是模棱两可的示例foo:bar
)。
-join($args|% t*y|%{"$_"|%('ToU*r','ToL*r')[+$l];$l=!('# -./&'|% Co*s $_)})
另请参阅@Iszi中具有内置功能ToTitleCase的Powershell解决方案。
解释测试脚本:
$f = {
-join(
$args|% t*y|%{ # for each char from input string
"$_"|%('ToU*r','ToL*r')[+$l] # evaluate ToUpper or ToLower method depend on $l (lower) value
# Note the variable $l is not defined for a first char
# so it evaluates toUpper for the first char
$l=!('# -./&'|% Co*s $_) # golfed form for $l=!'# -./&'.Contains($_) expression
# =0 if the current char is specified separator (toUpper for next char)
} # =1 otherwise (toLower for next char)
) # finally, join all chars to a result string
}
@(
,('foo:bar' ,'Foo:bar')
,('aGeNT ACcEpT/LEngTh-tYPe USeR raNgE.TyPe' ,'Agent Accept/Length-Type User Range.Type')
,('type&AgeNt/CC/COnteNt lEnGth#acCePT/AgeNt.RAnGe-Cc/contEnt/cODe' ,'Type&Agent/Cc/Content Length#Accept/Agent.Range-Cc/Content/Code')
,('cc/rEspoNCe.lEngtH#tYpE-witH&UsEr/bEgIN&uSer.AGEnT&begIn/aCCEPt/Cc' ,'Cc/Responce.Length#Type-With&User/Begin&User.Agent&Begin/Accept/Cc')
,('lENgTH#USeR.tYpE/BeGiN&LENGth tYpe/ACCEpt#rANge/codE&AnD-ACCepT/ConTenT' ,'Length#User.Type/Begin&Length Type/Accept#Range/Code&And-Accept/Content')
,('contENT/ACcEpT' ,'Content/Accept')
,('BeGin/wITH.tyPE&conTeNt' ,'Begin/With.Type&Content')
,('Code.cc#User.lenGTh-USer-AND&tyPE TypE&leNgtH.tYPe usER.UseR&with' ,'Code.Cc#User.Length-User-And&Type Type&Length.Type User.User&With')
,('RaNgE&COnTeNT WITh CoNTENT-TypE tyPe' ,'Range&Content With Content-Type Type')
,('BEgin COdE#uSeR#aGeNt.USeR' ,'Begin Code#User#Agent.User')
,('TypE LENGth' ,'Type Length')
) | % {
$s,$e = $_
$r = &$f $s
"$($r-ceq$e): $r"
}
输出:
True: Foo:bar
True: Agent Accept/Length-Type User Range.Type
True: Type&Agent/Cc/Content Length#Accept/Agent.Range-Cc/Content/Code
True: Cc/Responce.Length#Type-With&User/Begin&User.Agent&Begin/Accept/Cc
True: Length#User.Type/Begin&Length Type/Accept#Range/Code&And-Accept/Content
True: Content/Accept
True: Begin/With.Type&Content
True: Code.Cc#User.Length-User-And&Type Type&Length.Type User.User&With
True: Range&Content With Content-Type Type
True: Begin Code#User#Agent.User
True: Type Length