开发者

Can I apply word-casing to a string using regular expressions?

开发者 https://www.devze.com 2023-01-17 11:55 出处:网络
Is it possible to convert an all-uppercase string into a string where only the first letter of each word is in upper case using regular expressions?

Is it possible to convert an all-uppercase string into a string where only the first letter of each word is in upper case using regular expressions?

THIS IS A SAMPLE STRING ---> This Is A Sample String

开发者_运维知识库

At first I thought this would be an easy task, but now I don't even know how to start or even if it is possible.


In Perl:

$string =~ s/([\w']+)/\u\L$1/g;

(taken from the Perl FAQ)


No, in most languages you can't use regular expressions to do that. An exception to this is Perl which has a particularly powerful "regular" expression syntax.

You will probably find that your language has a library function that can do it. Look for something like s.titlecase().

Related:

  • Format string to title case
0

精彩评论

暂无评论...
验证码 换一张
取 消