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

时间:2016-07-31 16:45:17

标签: string vbscript capitalize

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

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

以下是此前使用的代码以及调用函数本身的代码: 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和The Giant Peach(例如)。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

使用正确的工具

  1. 字符串替换" - " => " "
  2. 带有Caps的替换功能的RegExp
  3. 例外字典
  4. 你会得到:

    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
    

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

相关问题