gpt4 book ai didi

powershell - 将字符串中的特定单词大写-Powershell

转载 作者:行者123 更新时间:2023-12-03 00:23:25 26 4
gpt4 key购买 nike

无论如何,我都需要能够使用一个句子并将其转换为第一个单词,每个单词都大写,但以下单词除外:to,a,the,at,in,of,with和but,或

例如:“你好,你好吗”所需结果:“你好,你好吗”

现在,我知道这看起来像是家庭作业,但现在我需要通过查看正确的脚本用法来学习。为了解决这个问题,我们付出了很多努力,但是我需要有人通过向我展示正确的方法来弥合差距……然后,我可以对其进行回顾并从中学习。

最佳答案

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

关于powershell - 将字符串中的特定单词大写-Powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31644042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com