使用经典 asp 检查字符串是否全部大写

Check if string is all capitals using classic asp

我需要一个函数来使用经典 asp 检查字符串是否全部(或大部分)大写。 (我需要防止用户使用所有大写输入标题。)

例如,如果一个 30 个字母的字符串包含 20 个或更多个大写字母,我需要将其标记为 "All capitals"。所以 "The Count of Monte Cristo" 可以,但 "The COUNT of MONTE CRISTO" 不行。

我想从匹配 [^A-Z] 的字母数开始,但我该怎么做?

这需要在经典 ASP 而不是 VB。

简单使用UCase函数

<%

dim a 
a = "This is a test 1"
dim b 
b = "THIS IS A TEST 2"

If a = ucase(a) then response.write(a & " is all upper")
If b = ucase(b) then response.write(b & " is all upper")

%>

结果

THIS IS A TEST 2 is all upper

与 UCase(input) 比较使其成为全有或全无检查;我更愿意查看 UCase 比率:

Option Explicit

Function Ucasity(s)
  If Len(s) Then
     Dim r : Set r = New RegExp
     r.Global = True
     r.Pattern = "[A-Z]"
     Dim m : Set m = r.Execute(s)
     Ucasity = m.Count / Len(s)
  Else
     Ucasity = 0
  End If
End Function

Function qq(s) : qq = """" & s & """" : End Function

Dim s
For Each s In Array( _
     "UPPERCASE but not ALL OR NOTHING" _
   , "UPPERCASE" _
   , "pipapo" _
   , "UPPERCASEuppercase" _
   , "" _
)
   WScript.Echo qq(s), CStr(s = UCase(s)), UCasity(s)
Next

输出:

cscript 39261181.vbs
"UPPERCASE but not ALL OR NOTHING" False 0,65625
"UPPERCASE" True 1
"pipapo" False 0
"UPPERCASEuppercase" False 0,5
"" True 0