【发布时间】: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