String VBScript-将每个单词的第一个字母大写

String VBScript-将每个单词的第一个字母大写,string,vbscript,capitalize,String,Vbscript,Capitalize,我试图将下面的字符串james和大桃子转换为以下内容:james和大桃子,因此基本上是交换一个空格,并将每个单词的第一个字母大写,除了and、the、or等词 我已经尝试了几个例子,可以做一个简单的替换来摆脱-对于一个空格,但我正在努力将每个单词的每个起始字母转换为大写 下面是在此之前使用的代码以及调用函数本身的代码: strModpack=Replace(Modpack,“-”,“”)strModpack=MakeUpperCase(strModpack) 以下是我尝试开始使用的代码: Fun

我试图将下面的字符串
james和大桃子
转换为以下内容:
james和大桃子
,因此基本上是交换一个空格,并将每个单词的第一个字母大写,除了and、the、or等词

我已经尝试了几个例子,可以做一个简单的替换来摆脱-对于一个空格,但我正在努力将每个单词的每个起始字母转换为大写

下面是在此之前使用的代码以及调用函数本身的代码:
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和巨桃

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

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”,必须将其更改为“-”分隔符,以作为示例输入

然后再回到《詹姆斯与大桃子》

无论如何,我认为这个小小的修改应该对你有用

最终版本

使用正确的工具

  • 字符串替换为“-”=>“”
  • 带有Caps替换函数的RegExp
  • 例外情况字典
  • 您将获得:

    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
    

    你需要一个大写字母的语法。

    我在代码的其他地方使用了
    strModpack=Replace(Modpack,“-”,”)
    ,但后来意识到
    strModpack=MakeUpperCase(Modpack)
    是错误的,它应该是
    strModpack=MakeUpperCase(strModpack)
    我现在已经纠正了这一点,并有了正确的案例,现在,只需包含代码,以对照不大写的单词进行检查。如果您问题中的代码不是您正在使用的代码,您应该对其进行编辑并为我们做一个记录-您是否尝试过上述代码?使用上述代码会在
    Const EXCLUDE_words As String=“the,a,and,or,was,is”的行中为expected=出现错误
    Ahh-vbscript不需要像VBA一样声明常量。只需将其更改为“Dim string”-我会更新代码啊,请在编辑获得批准后查看更新的代码,我根据您输入的内容进行了修补,几乎成功了,它只更改了我想从arrExclude改为小写的一个单词。
    cscript 38686250.vbs
    james-and-the-giant-peach
    James and the Giant Peach
    
    add-some-of-your-own
    Add Some Of Your Own