VBScript - 将每个单词的首字母大写

VBScript - Capitalize first letter of each word

我正在尝试将以下字符串 james-and-the-giant-peach 转换为以下字符串:James and the Giant Peach 所以基本上是将 - 换成 space,并将每个单词的首字母大写,除了像这样的单词和,或者等等

我已经尝试了几个例子,可以做一个简单的替换来摆脱 - for a space,但我很难将每个单词的每个起始字母转换为大写。

这是之前使用的代码和调用函数本身的代码: strModpack = Replace(Modpack,"-"," ") strModpack = MakeUpperCase ( strModpack )

这是我尝试开始的代码:

Function MakeUpperCase ( inputText )
  Dim arrWords, x, curWord
  Dim leftPart, rightPart
  arrWords = Split(inputText, " ")
    For x=0 To UBound(arrWords)
      curWord = arrWords(x)
    If Len(curWord)>0 Then
        leftPart = UCase(Left(curWord, 1))
        If Len(curWord)>1 Then
            rightPart = LCase(Right(curWord, Len(curWord) - 1))
        Else  
            rightPart = ""
        End If
        curWord = leftPart & rightPart

    End If
    arrWords(x) = curWord
    Next
       MakeUpperCase = Join(arrWords, " ")
    Erase arrWords
End Function

我目前的输出是:James and the giant peach

编辑:下面的代码看起来很接近,但只有一个单词需要小写。

Function MakeUpperCase(inputText)

Dim arrWords, x, curWord
Dim leftPart, rightPart

Exclude = "and,the"
arrExclude = Split ( Exclude, "," )
arrWords = Split ( inputText, " " )

 For x=0 To UBound(arrWords)
  curWord = arrWords(x)
   If Len(curWord)>0 Then
      leftPart = UCase(Left(curWord, 1))
   If Len(curWord)>1 Then
       rightPart = LCase(Right(curWord, Len(curWord) - 1))
    If curWord = arrExclude(intWord) Then           
        leftPart = LCase(leftPart)
    End if
    Else  
       rightPart = ""
   End If
 curWord = leftPart & rightPart

End If
arrWords(x) = curWord
Next
   MakeUpperCase = Join(arrWords, " ")
Erase arrWords

End Function

当前输出是:James and The Giant Peach(例如)。

如果您真的使用 arrWords = Split(inputText, " "),则您当前的输出不是 "James and the giant peach" - 必须将其更改为“-”定界符 - 无论如何,对于您的示例输入。

然后又回到James And The Giant Peach

无论如何 - 我认为这个轻微的修改应该适合你。

FINAL VERSION

 '  CONVERT TO UPPERCASE FUNCTION ------------------------ '
 Function MakeUpperCase(inputText)

  Dim arrWords, x, curWord
  Dim leftPart, rightPart

Exclude = "of,the"
arrExclude = Split ( Exclude, "," )
arrWords = Split ( inputText, " " )

 For x=0 To UBound(arrWords)
curWord = arrWords(x)
If Len(curWord)>0 Then
   leftPart = UCase(Left(curWord, 1))

        If Len(curWord)>1 Then
            rightPart = LCase(Right(curWord, Len(curWord) - 1))

            For intWord = 0 to UBound(arrExclude)
                If curWord = arrExclude(intWord) Then           
                    leftPart = LCase(leftPart)
                end if
            Next
        Else  
           rightPart = ""
        End If

        curWord = leftPart & rightPart

    End If
    arrWords(x) = curWord
Next

MakeUpperCase = Join(arrWords, " ")
Erase arrWords

End Function

使用正确的工具

  1. 字符串替换为“-”=>“”
  2. 具有大写替换功能的正则表达式
  3. 例外词典

你会得到:

Option Explicit

' replace function for 'words', heads, and tails
Function rf(sm, g1, g2, g3, np, ss)
  If Not gdLowers.Exists(g1) Then g1 = UCase(g2) & g3
  rf = g1
End Function

Dim s
' dictionary to hold words that stay lower case
Dim gdLowers : Set gdLowers = CreateObject("Scripting.Dictionary")
For Each s In Split("a the and")
    gdLowers(s) = Empty
Next

Dim f : Set f = GetRef("rf")
Dim r : Set r = New RegExp
r.Global = True
r.Pattern = "\b((.)(.*?))\b"
Dim aTests : aTests = Array( _
    "james-and-the-giant-peach" _
  , "add-some-of-your-own" _
)

For Each s In aTests
    WScript.Echo s
    WScript.Echo r.Replace(Replace(s, "-", " "), f)
    WScript.Echo
Next

输出:

cscript 38686250.vbs
james-and-the-giant-peach
James and the Giant Peach

add-some-of-your-own
Add Some Of Your Own

以及您需要 正确 大写字母语法的见解。