2014-09-11 145 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'. 
相关问题