在Perl中,如何将每个单词的首字母大写?

时间:2022-06-25 02:49:10

What is the easiest way to capitalize the first letter in each word of a string?

用字符串的每个单词的第一个字母大写的最简单的方法是什么?

10 个解决方案

#1


14  

See the faq.

查看常见问题。

I don't believe ucfirst() satisfies the OP's question to capitalize the first letter of each word in a string without splitting the string and joining it later.

我不相信ucfirst()满足OP的问题,将每个单词的第一个字母大写,而不拆分字符串并随后加入。

#2


46  

As @brian is mentioning in the comments the currently accepted answer by @piCookie is wrong!

正如@brian在评论中提到的,@piCookie目前接受的答案是错误的!

$_="what's the wrong answer?";
s/\b(\w)/\U$1/g
print; 

This will print "What'S The Wrong Answer?" notice the wrongly capitalized S

这将打印出“什么是错误的答案?”注意错误的大写S。

As the FAQ says you are probably better off using

正如FAQ中所说,你最好使用。

s/([\w']+)/\u\L$1/g

or Text::Autoformat

或文本::自动套用格式

#3


10  

Take a look at the ucfirst function.

看看ucfirst函数。

$line = join " ", map {ucfirst} split " ", $line;

#4


8  

$capitalized = join '', map { ucfirst lc } split /(\s+)/, $line;

By capturing the whitespace, it is inserted in the list and used to rebuild the original spacing. "ucfirst lc" capitalizes "teXT" to "Text".

通过捕获空格,它被插入到列表中,并用于重建原始的间距。“ucfirst lc”将“文本”资本化为“文本”。

#5


5  

$string =~ s/(\w+)/\u$1/g;

should work just fine

应该工作的很好

#6


2  

This capitalizes only the first word of each line:

这只大写了每一行的第一个单词:

perl -ne "print (ucfirst($1)$2)  if s/^(\w)(.*)/\1\2/" file

#7


1  

You can use 'Title Case', its a very cool piece of code written in Perl.

您可以使用“Title Case”,它是用Perl编写的非常酷的代码。

#8


0  

Note that the FAQ solution doesn't work if you have words that are in all-caps and you want them to be (only) capitalized instead. You can either make a more complicated regex, or just do a lc on the string before applying the FAQ solution.

请注意,FAQ解决方案如果你的单词是全大写的,并且你希望它们是大写的,那么它就不会起作用。您可以创建一个更复杂的regex,或者在应用FAQ解决方案之前在字符串上做一个lc。

#9


0  

The ucfirst function in a map certainly does this, but only in a very rudimentary way. If you want something a bit more sophisticated, have a look at John Gruber's TitleCase script.

地图上的ucfirst函数当然是这样做的,但只是在非常基本的方面。如果你想要更复杂一点的东西,可以看看约翰·格鲁伯的标题。

#10


0  

try this :

试试这个:

echo "what's the wrong answer?" |perl -pe 's/^/ /; s/\s(\w+)/ \u$1/g; s/^ //'

What's The Wrong Answer?

错误的答案是什么?

#1


14  

See the faq.

查看常见问题。

I don't believe ucfirst() satisfies the OP's question to capitalize the first letter of each word in a string without splitting the string and joining it later.

我不相信ucfirst()满足OP的问题,将每个单词的第一个字母大写,而不拆分字符串并随后加入。

#2


46  

As @brian is mentioning in the comments the currently accepted answer by @piCookie is wrong!

正如@brian在评论中提到的,@piCookie目前接受的答案是错误的!

$_="what's the wrong answer?";
s/\b(\w)/\U$1/g
print; 

This will print "What'S The Wrong Answer?" notice the wrongly capitalized S

这将打印出“什么是错误的答案?”注意错误的大写S。

As the FAQ says you are probably better off using

正如FAQ中所说,你最好使用。

s/([\w']+)/\u\L$1/g

or Text::Autoformat

或文本::自动套用格式

#3


10  

Take a look at the ucfirst function.

看看ucfirst函数。

$line = join " ", map {ucfirst} split " ", $line;

#4


8  

$capitalized = join '', map { ucfirst lc } split /(\s+)/, $line;

By capturing the whitespace, it is inserted in the list and used to rebuild the original spacing. "ucfirst lc" capitalizes "teXT" to "Text".

通过捕获空格,它被插入到列表中,并用于重建原始的间距。“ucfirst lc”将“文本”资本化为“文本”。

#5


5  

$string =~ s/(\w+)/\u$1/g;

should work just fine

应该工作的很好

#6


2  

This capitalizes only the first word of each line:

这只大写了每一行的第一个单词:

perl -ne "print (ucfirst($1)$2)  if s/^(\w)(.*)/\1\2/" file

#7


1  

You can use 'Title Case', its a very cool piece of code written in Perl.

您可以使用“Title Case”,它是用Perl编写的非常酷的代码。

#8


0  

Note that the FAQ solution doesn't work if you have words that are in all-caps and you want them to be (only) capitalized instead. You can either make a more complicated regex, or just do a lc on the string before applying the FAQ solution.

请注意,FAQ解决方案如果你的单词是全大写的,并且你希望它们是大写的,那么它就不会起作用。您可以创建一个更复杂的regex,或者在应用FAQ解决方案之前在字符串上做一个lc。

#9


0  

The ucfirst function in a map certainly does this, but only in a very rudimentary way. If you want something a bit more sophisticated, have a look at John Gruber's TitleCase script.

地图上的ucfirst函数当然是这样做的,但只是在非常基本的方面。如果你想要更复杂一点的东西,可以看看约翰·格鲁伯的标题。

#10


0  

try this :

试试这个:

echo "what's the wrong answer?" |perl -pe 's/^/ /; s/\s(\w+)/ \u$1/g; s/^ //'

What's The Wrong Answer?

错误的答案是什么?