【问题标题】:VBScript - Capitalize first letter of each wordVBScript - 每个单词的首字母大写
【发布时间】:2016-12-05 18:58:03
【问题描述】:

我正在尝试将以下字符串 james-and-the-giant-peach 转换为以下内容:James and the Giant Peach 所以基本上交换 - 一个空格,并将每个单词的第一个字母大写,除了和、the 或等单词。

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

这是之前使用的代码以及调用函数本身的代码: 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

目前的输出是:詹姆斯和大桃子(例如)。

【问题讨论】:

    标签: string vbscript capitalize


    【解决方案1】:

    如果您真的使用arrWords = Split(inputText, " "),那么您当前的输出不是“詹姆斯和大桃子” - 无论如何都必须将其更改为“-”分隔符 - 用于您的示例输入。

    然后又回到James And The Giant Peach

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

    最终版本

     '  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
    

    【讨论】:

    • 我在代码的其他地方使用了strModpack = Replace(Modpack,"-"," "),但后来意识到strModpack = MakeUpperCase ( Modpack )是错误的,它应该是strModpack = MakeUpperCase ( strModpack )我现在已经更正了这个问题并有了正确的案例,现在只是为了包含代码以检查单词不大写。
    • 如果您问题中的代码不是您正在使用的代码,您应该编辑它并为我们记下它 - 您是否尝试过上面的代码?
    • 使用上面的代码会给我一个预期的错误 = 在Const EXCLUDE_WORDS As String = "the,a,and,or,was,is"的行上
    • 啊 - vbscript 不需要像 VBA 那样声明常量。只需将其更改为“Dim string” - 我将更新代码
    • 啊,请在编辑获得批准后查看更新的代码,我根据您输入的内容进行了修改,几乎得到它,它只改变了我想从 arrExclude 小写的单词之一。跨度>
    【解决方案2】:

    使用正确的工具

    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
    

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

    【讨论】:

      猜你喜欢
      • 2020-08-30
      • 2015-11-10
      • 2016-12-23
      • 2015-11-04
      • 2017-08-02
      • 2012-11-11
      • 1970-01-01
      • 2013-12-15
      • 2012-07-24
      相关资源
      最近更新 更多