Vbscript 除连词和介词外,字符串大写

Vbscript 除连词和介词外,字符串大写,vbscript,asp-classic,Vbscript,Asp Classic,我使用下面的代码使字符串中每个单词的第一个字母都有大写字母。我想更进一步,只大写所有不是介词或连词(the,and,an,as,to)等的词。这在经典ASP中可能吗 将其转换为: 这是网上最好的网站 为此: 这是网上最好的网站 我认为这在正则表达式中是可能的,但我不知道从哪里开始。感谢您的帮助 queryForHTML=capCase(queryForHTML,true) 经典ASP中没有任何内置的东西可以为您做到这一点 我能想到的唯一选择是建立一个不应该大写的单词词典,并修改代码以不包含这

我使用下面的代码使字符串中每个单词的第一个字母都有大写字母。我想更进一步,只大写所有不是介词或连词(the,and,an,as,to)等的词。这在经典ASP中可能吗

将其转换为:

这是网上最好的网站
为此:

这是网上最好的网站
我认为这在正则表达式中是可能的,但我不知道从哪里开始。感谢您的帮助

queryForHTML=capCase(queryForHTML,true)

经典ASP中没有任何内置的东西可以为您做到这一点


我能想到的唯一选择是建立一个不应该大写的单词词典,并修改代码以不包含这些单词

这是我的第一个想法。我相信你能改进这一点

<%

    xText = "This Is The Best Website On The Web"    

    xTextSplit = split(xText, " ")

    for each item in xTextSplit

        xWord = item

        if lcase(item) = "the" or lcase(item) = "and" or lcase(item) = "an" or lcase(item) = "as" or lcase(item) = "to" or lcase(item) = "is" or lcase(item) = "on" then
            xWord = lcase(item)
        end if

        xCompleteWord = xCompleteWord &" "& xWord
    next

    response.write xCompleteWord
%>

您可以使用
哈希集(字符串)
来存储这些异常单词。然后按空格分割以获取字符串中的所有单词,检查是否需要将第一个字母大写或小写,并使用
string.Join
创建新字符串

以下是一种实现此功能的方法:

Private Shared ReadOnly CapCaseExceptions As New HashSet(Of String)(StringComparer.CurrentCultureIgnoreCase) From {
    "the", "and", "an", "as", "to", "is", "on"
} ' etc.

Public Shared Function CapCase(input As String) As String
    Dim words = From w In input.Split()
                Let word = If(CapCaseExceptions.Contains(w),
                              Char.ToLower(w(0)) + w.Substring(1),
                              Char.ToUpper(w(0)) + w.Substring(1))
                Select word
    Return String.Join(" ", words)
End Function
您的示例输入:

Dim input As String = "This Is The Best Webite On The Web"
Console.Write(CapCase(input)) ' This is the Best Webite on the Web

编辑:我不熟悉经典ASP,所以我不知道它是否有用。

好的,这是构建的基础。如果第一个单词以lcase中的一个项目开头呢?如何确保查询中的第一个单词始终为大写?执行计数器并
如果此计数器=1,则xWord=ucase(左(项,1))&右(项,列(项)-1)
。这将在第一项上强制使用资本。这是
Dim input As String = "This Is The Best Webite On The Web"
Console.Write(CapCase(input)) ' This is the Best Webite on the Web