将字符串中的特定单词大写-Powershell
在任何情况下,我都需要能够把一个句子转换成第一个单词,每个单词都大写,除了以下单词:to,a,the,at,in,of,with,and,but,or 示例:“你好,丹”所需结果:你好,丹”将字符串中的特定单词大写-Powershell,powershell,Powershell,在任何情况下,我都需要能够把一个句子转换成第一个单词,每个单词都大写,除了以下单词:to,a,the,at,in,of,with,and,but,or 示例:“你好,丹”所需结果:你好,丹” 现在我知道这看起来像是家庭作业,但我现在需要通过查看正确的脚本用法来学习。已经投入了大量的努力来找出如何做到这一点,但我需要有人通过向我展示正确的方法来弥合差距……然后我可以回顾它并从中学习。我不会给你一个完整的脚本,但我可以希望这能让你走上正轨 首先,创建一个字符串数组,其中包含所有不希望大写的单词 将输
现在我知道这看起来像是家庭作业,但我现在需要通过查看正确的脚本用法来学习。已经投入了大量的努力来找出如何做到这一点,但我需要有人通过向我展示正确的方法来弥合差距……然后我可以回顾它并从中学习。我不会给你一个完整的脚本,但我可以希望这能让你走上正轨 首先,创建一个字符串数组,其中包含所有不希望大写的单词 将输入字符串(
'hello how you dan'
)按空格分割。最后的数组应类似于'hello'、'how'、'are'…
在拆分字符串中循环,查看单词是否在您创建的第一个数组中
如果是,则忽略它,但如果不是,则希望取第一个字母,并使用string方法确保它是大写的
然后,您需要重新连接字符串(不要忘记空格)。您可以在拆分数组中循环时重新构建字符串以备输出,也可以在最后重新构建字符串
(强调添加了对您将要搜索的某些关键字的提示。)我不会给您一个完整的脚本,但我可以对其进行伪编码,希望能让您走上正确的道路 首先,创建一个字符串数组,其中包含所有不希望大写的单词 将输入字符串(
'hello how you dan'
)按空格分割。最后的数组应类似于'hello'、'how'、'are'…
在拆分字符串中循环,查看单词是否在您创建的第一个数组中
如果是,则忽略它,但如果不是,则希望取第一个字母,并使用string方法确保它是大写的
然后,您需要重新连接字符串(不要忘记空格)。您可以在拆分数组中循环时重新构建字符串以备输出,也可以在最后重新构建字符串
(在提示您将要查找的某些关键字时添加了强调语。)Windos的答案很准确,但我很无聊,所以这里是一个完全可行的实现:
function Get-CustomTitleCase {
param(
[string]$InputString
)
$NoCapitalization = @(
'are',
'to',
'a',
'the',
'at',
'in',
'of',
'with',
'and',
'but',
'or')
( $InputString -split " " |ForEach-Object {
if($_ -notin $NoCapitalization){
"$([char]::ToUpper($_[0]))$($_.Substring(1))"
} else { $_ }
}) -join " "
}
像这样使用它:
PS C:\> Get-CustomTitleCase "hello, how are you dan"
Hello, How are You Dan
Windos的答案很准确,但我很无聊,所以这里有一个完全可以工作的实现:
function Get-CustomTitleCase {
param(
[string]$InputString
)
$NoCapitalization = @(
'are',
'to',
'a',
'the',
'at',
'in',
'of',
'with',
'and',
'but',
'or')
( $InputString -split " " |ForEach-Object {
if($_ -notin $NoCapitalization){
"$([char]::ToUpper($_[0]))$($_.Substring(1))"
} else { $_ }
}) -join " "
}
像这样使用它:
PS C:\> Get-CustomTitleCase "hello, how are you dan"
Hello, How are You Dan
正则表达式解释:
\b #Start at the beginning of a word.
(?!(are|to|a|the|at|in|of|with|and|but|or) #match only if a word does not begin with "to, a, the, at, in, of, with, and, but, or"
\b #Second \b to signify that there are no characters after the words listed in the negative lookahead list.
\w #Match any single word character
$letter.Value.ToUpper() # convert the matched letter(value) to uppercase
正则表达式解释:
\b #Start at the beginning of a word.
(?!(are|to|a|the|at|in|of|with|and|but|or) #match only if a word does not begin with "to, a, the, at, in, of, with, and, but, or"
\b #Second \b to signify that there are no characters after the words listed in the negative lookahead list.
\w #Match any single word character
$letter.Value.ToUpper() # convert the matched letter(value) to uppercase
“是”不在例外列表中,但您希望它不会大写?抱歉,我忘了在示例单词列表中添加不大写第一个字母“是”的内容“不在您的例外列表中,但您希望它不会大写吗?抱歉,我忘了将其添加到示例单词列表中,以不大写第一个字母
$string='toad,africa,theme,india,officer,withy,butter,orange'
@PetSerAl-yup它需要一个额外的\b来表示单词边界的结束。”。。反馈的thx:)$string='toad,africa,Theme,india,officer,withy,butter,orange'
@PetSerAl-是的,它需要一个额外的\b来表示单词边界的结束。。反馈的thx:)如果传入的字符串必须与相邻的空格连接怎么办?您最好将'
添加到$NoCapitalization
数组中。如果传入的字符串必须与相邻的空格对齐,该怎么办?您最好将'
添加到$NoCapitalization
数组中。