大写的单词串除了prepositional话 [英] Capitalize string of words except for prepositional words

查看:204
本文介绍了大写的单词串除了prepositional话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是code以下采取从一个文本框中输入一个字符串,并将其转换为资本的情况下,除了像字(的,并且,一个作为,以或上)等

问题1:我想字符串的第一个字总是大写的,无论这个词是什么

问题2:当字符串被放回一起字间距不正确

  XTEXT = queryForHTML
xTextSplit =拆分(XTEXT,)在xTextSplit每个项目    xWord =项目    如果LCASE(项目)=中的或LCASE(项目)=和或LCASE(项目)=是或LCASE(项目)=是或LCASE(项目)=到或LCASE(项目) =是或LCASE(项目)=上,那么
        xWord = LCASE(项目)
    万一    xCompleteWord = xCompleteWord&安培; 与& xWord下一个queryForHTML = xCompleteWord


解决方案

 显式的选项昏暗originalString
    originalString =字符串大小写的样本(在某些情况下,不那么明显)昏暗convertedString昏暗noiseWords
    noiseWords =/ A /后于/这艘/约/以上/无/横跨/前述/后/反对/沿着/旁边/际+ _
                /烟雨/中/之间/ AN / anenst /中肯/ APUD /前后/为/预留/横跨/在/横过/之上+ _
                /禁止/前/后面/低于/下/旁边/此外/间/超越/但/ BY /大约+ _
                /关于/​​虽然/下/中/除/不含/失败/下/为/ forenenst /从+ _
                /给出/中/包括/在/到/像/中/中间/负/模数/近一个/下/尽管+ _
                / O /开/关/开/到/相反/或输入/输出/外/过/速度/过去/元/加/ PRO / QUA /关于+ _
                /轮/ SANS /保存/因为/所以/不是/经/ THRU /整个/ thruout /钱柜/次/到/朝+ _
                /对/下/下/不像/直到/对/上/在/与/ VS /经/副/ VIS /带/内+ _
                /无/价值/本/    功能correctCase(matchString,文字,位置,sourceString)
        字= LCASE(字)
        如果(位置大于0)和(INSTR(noiseWords,/&放大器;字放大器;/)大于0)然后
            correctCase =字
        其他
            correctCase =用Ucase(左(字,1))及MID(文字,2,LEN(字)-1)
        万一
    结束功能    随着新的RegExp
        .Pattern =(\\ w +)
        。全球= TRUE
        .IgnoreCase = TRUE
        convertedString = .Replace(originalString,GetRef(correctCase))
    结束与    WScript.Echo originalString
    WScript.Echo convertedString

其基本思想是用一个常规的前pression匹配的字字符的任意序列( [A-ZA-Z0-9] )和每个序列,调用一个函数接收作为参数字符串匹配,包含文字,它已被找到字符串中的地位和完整的源字符串的捕获组。

如果这个词是在位置0开始资本化。如果字是一个噪音字,它是小写字母,否则,这个词是大写。

I am using the code below to take a string entered from a text box, and convert to capital case, except for words like (the, and, an, as, to, or, on) etc.

Issue #1: I want the first word of the string to always be capitalized regardless of what the word is.

Issue #2: The word spacing is not correct when the string is put back together.

xText = queryForHTML    
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

queryForHTML = xCompleteWord

解决方案

Option Explicit 

Dim originalString    
    originalString = "a saMple of String capiTalization (in some cases, not so obvious)"

Dim convertedString

Dim noiseWords
    noiseWords= "/a/abaft/aboard/about/above/absent/across/afore/after/against/along/alongside/amid" + _ 
                "/amidst/among/amongst/an/anenst/apropos/apud/around/as/aside/astride/at/athwart/atop" + _ 
                "/barring/before/behind/below/beneath/beside/besides/between/beyond/but/by/circa" + _ 
                "/concerning/despite/down/during/except/excluding/failing/following/for/forenenst/from" + _ 
                "/given/in/including/inside/into/like/mid/midst/minus/modulo/near/next/notwithstanding" + _ 
                "/o/of/off/on/onto/opposite/or/out/outside/over/pace/past/per/plus/pro/qua/regarding" + _ 
                "/round/sans/save/since/so/than/through/thru/throughout/thruout/till/times/to/toward" + _ 
                "/towards/under/underneath/unlike/until/unto/up/upon/versus/vs/via/vice/vis/with/within" + _ 
                "/without/worth/this/"

    Function correctCase(matchString,word,position,sourceString)
        word = LCase(word)
        If (position > 0) And (InStr(noiseWords,"/" & word & "/")>0) Then 
            correctCase = word
        Else
            correctCase = UCase(Left(word,1)) & Mid(word,2,Len(word)-1)
        End If
    End Function 

    With New RegExp
        .Pattern = "(\w+)"
        .Global = True 
        .IgnoreCase = True
        convertedString = .Replace(originalString,GetRef("correctCase"))
    End With 

    WScript.Echo originalString
    WScript.Echo convertedString

The basic idea is to use a regular expression matching any sequence of "word" characters ([a-zA-Z0-9]) and for each sequence, a function is called which receives as parameters the string matches, the capture group containing the word, the position in the string where it has been found and the full source string.

If the word is at position 0 it is capitalized. If the word is a "noise" word, it is lowercased, else, the word is capitalized.

这篇关于大写的单词串除了prepositional话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆