例如“医学主任”,我希望它是“医学主任而非医学主任”,我不希望字母“of”被大写,请帮助如何将每个字母的字母变成大写字母,但不是字母“of”,“and”,“it”,“for”?
3
A
回答
1
以下VBA代码将是良好的开端
Option Base 1
Option Explicit
Function ProperIsh(inputString As String) As String
Dim result As String
Dim currWord As String
Dim idx As Integer
Dim wordPos As Integer
' List of words to revert to lower-case '
Dim lowerWords As Variant
lowerWords = Array("Of", "And", "It", "For", "Am", "The")
' Get proper-cased string with spaces on either end '
result = " " & WorksheetFunction.Proper(inputString) & " "
' Process each word to revert to lower-case '
For idx = 1 To UBound(lowerWords)
' Revert every one of that word with spaces on either side '
currWord = " " & lowerWords(idx) & " "
wordPos = InStr(result, currWord)
While wordPos > 0
result = Left(result, wordPos - 1) & LCase(currWord) & Mid(result, wordPos + Len(currWord))
wordPos = InStr(result, currWord)
Wend
Next
' Get rid of the spaces at the end '
ProperIsh = Mid(result, 2, Len(result) - 2)
End Function
而对于它的一些测试代码:
Sub test()
MsgBox (ProperIsh("HELLO I AM THE LAW and i am the lower case law of everything"))
End Sub
它所做的是正确的情况下的每一个字(大写首字母,其他的一切小写),然后系统恢复任何特殊的词语都会回到所有的低位ER-情况。
它假设空间是唯一的分隔符,但如果是这样的话可以使其更具适应性。
测试代码生成一个消息框与预期的输出:
Hello I am the Law and I am the Lower Case Law of Everything
为了使用它在表达式中,把它作为任何其它用户定义的函数,例如具有:
=ProperIsh(A1)
您可以在运行中看到它,其中列B使用上面显示的公式:
A B
1 director of medicine Director of Medicine
2 I am the law I am the Law
3 Let slip the dogs of war Let Slip the Dogs of War
+1
当使用-ish作为后缀时,不需要大写; D –
0
我用Rules for Capitalization in Titles of Articles作为创建大写异常列表的参考。
Function TitleCase
使用WorksheetFunction.ProperCase
预处理文本。出于这个原因,我对收缩进行了例外,因为WorksheetFunction.ProperCase
不恰当地利用它们。
每个句子的第一个单词和双引号之后的第一个单词将保持大写。标点符号也可以正确处理。
Function TitleCase(text As String) As String
Dim doc
Dim sentence, word, w
Dim i As Long, j As Integer
Dim arrLowerCaseWords
arrLowerCaseWords = Array("a", "an", "and", "as", "at", "but", "by", "for", "in", "of", "on", "or", "the", "to", "up", "nor", "it", "am", "is")
text = WorksheetFunction.Proper(text)
Set doc = CreateObject("Word.Document")
doc.Range.text = text
For Each sentence In doc.Sentences
For i = 2 To sentence.Words.Count
If sentence.Words.Item(i - 1) <> """" Then
Set w = sentence.Words.Item(i)
For Each word In arrLowerCaseWords
If LCase(Trim(w)) = word Then
w.text = LCase(w.text)
End If
j = InStr(w.text, "'")
If j Then w.text = Left(w.text, j) & LCase(Right(w.text, Len(w.text) - j))
Next
End If
Next
Next
TitleCase = doc.Range.text
doc.Close False
Set doc = Nothing
End Function
相关问题
- 1. 大写字母的第一个字母
- 2. 在每个大写字母后加小写字母后加小写字母
- 3. C#计数每个大写字母和每个小写字母
- 4. 如何写一个字母的字母?
- 5. 大写字母和小写字母
- 6. X86 NASM将大写字母转换为大写字母和小写字母
- 7. 如何小写字母转换为大写字母和大写字母为小写字母
- 8. Swift大写字母中每个单词的第一个字母
- 9. 用大写字母替换“ - ”字母
- 10. 字体小写字母,只是较小的大写字母
- 11. 如何仅将大写字母的首字母大写,而将其余字母大写?
- 12. VB.net我如何使大写字母是小写字母
- 13. LUA中的大写字母功能但非字母字符?
- 14. 用小写字母写成小写字母大写
- 15. 如何将句首字母的首字母大写?
- 16. Sed不能将大写字母转换为小写字母
- 17. 首字母大写的字符串首字母大写
- 18. Javascript/jQuery大写变音字母的第一个字母
- 19. C++,接受变量中的小写字母和大写字母
- 20. 大写字母的字符串的第一个字母只
- 21. 如何识别TSQL中的大写字母和小写字母?
- 22. 如何匹配的第一个字母是大写/大写字母在PHP?
- 23. 如何将每个句子的第一个字母转换为大写字母,将所有其他字母转换为小写字母?
- 24. 字符串 - 大写元音,下一个字母的字母 - Javascript
- 25. preg_match字母不仅字母?
- 26. .htaccess将大写字母转换为小写字母的目录
- 27. 大写字母,小写字母,大写的Ant属性
- 28. 带大写字母和小写字母的字符串转换
- 29. 接受大写和小写字母的字母数字约束
- 30. 如何忽略小写字母或大写字母?
我们不是心理学家,就像我们想成为的那样。你不给任何背景或代码,也没有问题或细节。目前尚不清楚。我们无法帮助你。 – Li357