如何将字符串中每个单词的首字母大写?使用1U2
作为replace()
的一部分会引发错误:Bad replacement string
。建议使用正则表达式,但欢迎使用其他方式。这就是我期望的工作,但给出了错误:
test_string = "the quick brown fox jumps over the lazy dog"
replace(test_string, r"(^|s)([a-z])" => s"1U2")
你可以像这样使用titlecase
函数:
julia> test_string = "the quick brown fox jumps over the lazy dog"
"the quick brown fox jumps over the lazy dog"
julia> titlecase(test_string, strict=false)
"The Quick Brown Fox Jumps Over The Lazy Dog"
对于更复杂的情况,您可以定义wordsep
函数或将strict
更改为true
(默认值(。
下面是使用replace
、uppercase
和正则表达式的另一个选项:
replace(test_string, r"(^|s)([a-z])" => uppercase)