0

嗨,我是 VB 脚本的新手,遇到了一个问题,问题是这样的。

我在这个字符串中有一个字符串(例如:- xx xxxxx xxxx xx xxxx xx)我必须将备用字符转换为大写,但不应考虑单词之间的空格。

字符串的输出应该是这样的(例如:- xX xXxXx xXxX xX xXxX xX)

为此,我尝试了“Mid”和“Ucase”功能,但在这方面它也在考虑空间,所以我的输出不符合我的例外。

这个你能帮我吗....

4

2 回答 2

1

使用带有模式的正则表达式查找可选空格和两个符号以及执行 UCase() 的替换回调函数 - 如下所示:

Dim aTests : aTests = Array( _
      Array("", "") _
    , Array("xx", "xX") _
    , Array("aB cD", "aB cD") _
    , Array("xx xxxxx xxxx xx xxxx xx", "xX xXxXx xXxX xX xXxX xX")_
    , Array(" ab cd", " aB cD") _
  )
  Dim re : Set re = New RegExp
  re.Global = True
  re.Pattern = " ?.."
  Dim rf : Set rf = GetRef("ReRpl")
  Dim aTest
  For Each aTest In aTests
      WScript.Echo "-----", qq(aTest(0))
      Dim sRes : sRes = re.Replace(aTest(0), rf)
      If sRes = aTest(1) Then
         WScript.Echo "   ok", qq(sRes)
      Else
         WScript.Echo "  res", qq(sRes)
         WScript.Echo "  exp", qq(aTest(1))
      End If
  Next

Function ReRpl(sM, nP, sS)
  Dim nL : nL = Len(sM)
  ReRpl = Left(sM, nL - 1) & UCase(Mid(sM, nL))
End Function

输出:

----- ""
   ok ""
----- "xx"
   ok "xX"
----- "aB cD"
   ok "aB cD"
----- "xx xxxxx xxxx xx xxxx xx"
   ok "xX xXxXx xXxX xX xXxX xX"
----- " ab cd"
   ok " aB cD"

请在投入生产之前添加更多测试用例;我不完全确定你的规格。

更新:

为了鼓励您制定规范(添加更多示例?),我添加了@Ansgar 的“循环字符”方法的两个版本:

Function Ucase2ndAW(s)
  Ucase2ndAW = ""
  Dim bUC : bUC = False
  Dim p
  For p = 1 To Len(s)
      Dim c : c = Mid(s, p, 1)
      If " " <> c Then
         If bUC Then c = UCase(c)
         bUC = Not bUC
      End If
      Ucase2ndAW = Ucase2ndAW & c
  Next
End Function
Function Ucase2ndEH(s)
  Ucase2ndEH = ""
  Dim bUC : bUC = False
  Dim p
  For p = 1 To Len(s)
      Dim c : c = Mid(s, p, 1)
      If " " <> c Then
         If bUC Then c = UCase(c)
         bUC = Not bUC
      Else
         bUC = False
      End If
      Ucase2ndEH = Ucase2ndEH & c
  Next
End Function

一个新的测试驱动程序:

  Dim aTests : aTests = getTests()
  Dim aTest
  Dim re     : Set re = New RegExp
  re.Global = True
  re.Pattern = "( ?.)(.)"
  Dim rf : Set rf = GetRef("ReRpl02")
  For Each aTest In aTests
      WScript.Echo "-----", qq(aTest(0))
      WScript.Echo "  exp", qq(aTest(1))
      WScript.Echo "   AW", qq(Ucase2ndAW(aTest(0)))
      WScript.Echo "   EH", qq(Ucase2ndEH(aTest(0)))
      WScript.Echo "   RE", qq(re.Replace(aTest(0), rf))
  Next

及其输出:

----- ""
  exp ""
   AW ""
   EH ""
   RE ""
----- "xx"
  exp "xX"
   AW "xX"
   EH "xX"
   RE "xX"
----- "aB cD"
  exp "aB cD"
   AW "aB cD"
   EH "aB cD"
   RE "aB cD"
----- "xx xxxxx xxxx xx xxxx xx"
  exp "xX xXxXx xXxX xX xXxX xX"
   AW "xX xXxXx XxXx Xx XxXx Xx"
   EH "xX xXxXx xXxX xX xXxX xX"
   RE "xX xXxXx xXxX xX xXxX xX"

忘记发布了

Function ReRpl02(sM, sG1, sG2, nP, sS)
  ReRpl02 = sG1 & UCase(sG2)
End Function

(让 RegExp 引擎进行匹配拆分要容易得多)

于 2013-05-27T06:47:34.960 回答
0

我会使用这样的东西:

s1 = "xx xxxxx xxxx xx xxxx xx"
s2 = ""

makeUpperCase = False
For i = 1 To Len(s1)
  c = Mid(s1, i, 1)
  If c = " " Then makeUpperCase = False
  If makeUpperCase Then c = UCase(c)
  s2 = s2 & c
  If c <> " " Then makeUpperCase = Not makeUpperCase
Next

WScript.Echo s2

如果您还必须处理非字母字符,则需要进行额外检查。

于 2013-05-27T08:46:33.003 回答