2014-09-11 196 views

回答

80

首字母大寫字符串:

"is There any other WAY".capitalize 
res8: String = Is There any other WAY 

大寫每個單詞的第一個字母的字符串:

"is There any other WAY".split(' ').map(_.capitalize).mkString(" ") 
res9: String = Is There Any Other WAY 

大寫字符串的第一個字母,而外殼下的一切:

"is There any other WAY".toLowerCase.capitalize 
res7: String = Is there any other way 

大寫每個單詞的第一個字母串,而外殼下的一切:

"is There any other WAY".toLowerCase.split(' ').map(_.capitalize).mkString(" ") 
res6: String = Is There Any Other Way 
7

有點令人費解,您可以使用拆分得到的字符串列表,然後利用資本,進而降低找回字符串:

scala> "is There any other WAY".split(" ").map(_.capitalize).mkString(" ") 
res5: String = Is There Any Other WAY 
0

儘管使用分隔符來大寫每個單詞的首字母:

scala> import com.ibm.icu.text.BreakIterator 
scala> import com.ibm.icu.lang.UCharacter 

scala> UCharacter.toTitleCase("is There any-other WAY", BreakIterator.getWordInstance) 
res33: String = Is There Any-Other Way 
0

無論分隔符如何,這一個都將大寫每個單詞,並且不需要任何額外的庫。它也會正確處理撇號。

scala> raw"\b((?<!\b')\w+)".r.replaceAllIn("this is a test, y'all! 'test/test'.", _.group(1).capitalize) 
res22: String = This Is A Test, Y'all! 'Test/Test'. 
相關問題