利用字符串除了连词和prepositions [英] Capitalize string except for conjunctions and prepositions

查看:118
本文介绍了利用字符串除了连词和prepositions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code键使字符串必须为每个单词的首字母大写。我想更进一步借此,只利用未prepositions,或连词所有单词(的,而且,一个作为,以)等这是可能在传统的ASP?

此转换:

这是在网络上最好的网站

要这样:

这是网络上最好的网站

我认为这是可能的正则表达式,但我没有在哪里与开始的线索。任何帮助是AP preciated。

queryForHTML = capCase(queryForHTML,真)


解决方案

您可以使用的HashSet(串)来存储这些特殊的词。然后,通过空格分割来获取所有文字字符串中,检查是否需要为大写或小写的第一个字母,并使用的string.join 以创建一个新的字符串。

下面是做它的方法:

 专用共享只读CapCaseExceptions作为新的HashSet(串)(StringComparer.CurrentCultureIgnoreCase)从{
    该,和,一个,为,到,是,上
}等。公共共享功能CapCase(输入作为字符串)作为字符串
    昏暗的话=从W IN input.Split()
                让字=如果(CapCaseExceptions.Contains(w)的
                              Char.ToLower(W(0))+ w.Substring(1),
                              Char.ToUpper(W(0))+ w.Substring(1))
                选择一个单词
    返回的string.join(,字)
结束功能

您样本输入:

 输入暗淡作为字符串=这是最好的Webite在网络上
Console.Write(CapCase(输入))这是最好的Webite在Web上

修改:我不熟悉的传统的ASP,所以我不知道这是否有助于

I am using the code below to make a string have capitalization for the first letter in each word. I would like to take this a step further, and only capitalize all words that are not prepositions, or conjunctions (the, and, an, as, to) etc. Is this possible in classic ASP?

Convert this:

this is the best website on the web

To this:

This is the Best Website on the Web

I would think this is possible with RegEx, but I don't have a clue on where to start with that. Any help is appreciated.

queryForHTML = capCase(queryForHTML,true)

解决方案

You could use a HashSet(Of String) to store those exceptional words. Then split by white-space to get all words in a string, check whether you need to upper- or lowercase the first letter and use string.Join to create a new string.

Here is a method that does it:

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

Your sample input:

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

Edit: i'm not familiar with classic ASP, so i don't know if it helps.

这篇关于利用字符串除了连词和prepositions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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