Julia 正则表达式,使下一个字符大写



如何将字符串中每个单词的首字母大写?使用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(默认值(。

下面是使用replaceuppercase和正则表达式的另一个选项:

replace(test_string, r"(^|s)([a-z])" => uppercase)

相关内容

最新更新