确定字符是否是VB6中的字母的最佳方法是什么?

时间:2021-11-15 20:23:29

Need a function that takes a character as a parameter and returns true if it is a letter.

需要一个将字符作为参数的函数,如果是字母则返回true。

9 个解决方案

#1


4  

Seanyboy's IsCharAlphaA answer is close. The best method is to use the W version like so:

Seanyboy的IsCharAlphaA答案很接近。最好的方法是使用W版本,如下所示:

Private Declare Function IsCharAlphaW Lib "user32" (ByVal cChar As Integer) As Long
Public Property Get IsLetter(character As String) As Boolean
    IsLetter = IsCharAlphaW(AscW(character))
End Property

Of course, this all rarely matters as all of VB6's controls are ANSI only

当然,这一切都很重要,因为VB6的所有控件都只有ANSI

#2


8  

This was part of the code posted by rpetrich in response to a question by Joel Spolsky. I felt it needed a post specific to the problem it solves. It really is brilliant.

这是rpetrich在回答Joel Spolsky提出的问题时发布的代码的一部分。我觉得它需要一个特定于它解决的问题的帖子。真的很棒。

Private Function IsLetter(ByVal character As String) As Boolean
    IsLetter = UCase$(character) <> LCase$(character)
End Function

You may be thinking to yourself, "Will this always work?" The documentation on the UCase and LCase functions, confirms that it will:

你可能会想,“这总能奏效吗?”有关UCase和LCase功能的文档证实它将:

UCase Function Only lowercase letters are converted to uppercase; all uppercase letters and nonletter characters remain unchanged.

UCase函数只有小写字母转换为大写;所有大写字母和非字母字符保持不变。

LCase Function Only uppercase letters are converted to lowercase; all lowercase letters and nonletter characters remain unchanged.

LCase功能只有大写字母转换为小写字母;所有小写​​字母和非字母字符保持不变。

#3


2  

Private Function IsLetter(Char As String) As Boolean
    IsLetter = UCase(Char) Like "[ABCDEFGHIJKLMNOPQRSTUVWXYZ]"
End Function

#4


1  

What's wrong with the following, which doesn't rely on obscure language behaviour?

以下是什么问题,它不依赖于模糊的语言行为?

Private Function IsLetter(ByVal ch As String) As Boolean
    IsLetter = (ch >= "A" and ch <= "Z") or (ch >= "a" and ch <= "z")
End Function

#5


1  

I believe we can improve upon this a little more. rpetrich's code will work, but perhaps only by luck. The API call's parameter should be a TCHAR (WCHAR here actually) and not a Long. This also means no fiddling with converting to a Long or masking with &HFFFF. This by the way is Integer and adds an implicit conversion to Long here too. Perhaps he meant &HFFFF& in this case?

我相信我们可以对此进行更多改进。 rpetrich的代码可以工作,但也许只能靠运气。 API调用的参数应该是TCHAR(实际上是WCHAR)而不是Long。这也意味着没有摆弄转换为Long或使用&HFFFF屏蔽。顺便说一句,这是Integer并在这里添加一个隐式转换为Long。在这种情况下,也许他的意思是&HFFFF?

On top of that it might be best to explictly call the UnicoWS wrapper for this API call, for Win9X compatibility. The UnicoWS.dll may need to be deployed but at least we gain that option. Then again maybe from VB6 this is automagically redirected, I don't have Win9X installed to test it.

最重要的是,最好为这个API调用明确地调用UnicoWS包装器,以实现Win9X兼容性。可能需要部署UnicoWS.dll,但至少我们获得了该选项。然后再次从VB6可以自动重定向,我没有安装Win9X来测试它。

Option Explicit

Private Declare Function IsCharAlphaW Lib "unicows" (ByVal WChar As Integer) As Long

Private Function IsLetter(Character As String) As Boolean
    IsLetter = IsCharAlphaW(AscW(Character))
End Function

Private Sub Main()
    MsgBox IsLetter("^")
    MsgBox IsLetter("A")
    MsgBox IsLetter(ChrW$(&H34F))
    MsgBox IsLetter(ChrW$(&HFEF0))
    MsgBox IsLetter(ChrW$(&HFEFC))
End Sub

#6


0  

Looking around a bit came up with the following...

看了一下有点以下......

Private Declare Function IsCharAlphaA Lib "user32" Alias "IsCharAlphaA" (ByVal cChar As Byte) As Long

I believe IsCharAlphaA tests ANSI character sets and IsCharAlpha tests ASCII. I may be wrong.

我相信IsCharAlphaA测试ANSI字符集和IsCharAlpha测试ASCII。我可能错了。

#7


0  

Private Function IsAlpha(ByVal vChar As String) As Boolean
  Const letters$ = "abcdefghijklmnopqrstuvwxyz"

  If InStr(1, letters, LCase$(vChar)) > 0 Then IsAlpha = True
End Function

#8


0  

I use this in VBA

我在VBA中使用它

Function IsLettersOnly(Value As String) As Boolean
   IsLettersOnly = Len(Value) > 0 And Not UCase(Value) Like "*[!A-Z]*"
End Function

#9


-1  

It doesn't exactly document itself. And it may be slow. It's a clever hack, but that's all it is. I'd be tempted to be more obvious in my checking. Either use regex's or write a more obvious test.

它并不完全记录自己。而且可能很慢。这是一个聪明的黑客,但就是这样。在我的检查中,我很想明白。要么使用正则表达式,要么写一个更明显的测试。

public bool IsAlpha(String strToCheck)
{
    Regex objAlphaPattern=new Regex("[^a-zA-Z]");
    return !objAlphaPattern.IsMatch(strToCheck);
}

public bool IsCharAlpha(char chToCheck)
{
    return ((chToCheck=>'a') and (chToCheck<='z')) or ((chToCheck=>'A') and (chToCheck<='Z'))
}

#1


4  

Seanyboy's IsCharAlphaA answer is close. The best method is to use the W version like so:

Seanyboy的IsCharAlphaA答案很接近。最好的方法是使用W版本,如下所示:

Private Declare Function IsCharAlphaW Lib "user32" (ByVal cChar As Integer) As Long
Public Property Get IsLetter(character As String) As Boolean
    IsLetter = IsCharAlphaW(AscW(character))
End Property

Of course, this all rarely matters as all of VB6's controls are ANSI only

当然,这一切都很重要,因为VB6的所有控件都只有ANSI

#2


8  

This was part of the code posted by rpetrich in response to a question by Joel Spolsky. I felt it needed a post specific to the problem it solves. It really is brilliant.

这是rpetrich在回答Joel Spolsky提出的问题时发布的代码的一部分。我觉得它需要一个特定于它解决的问题的帖子。真的很棒。

Private Function IsLetter(ByVal character As String) As Boolean
    IsLetter = UCase$(character) <> LCase$(character)
End Function

You may be thinking to yourself, "Will this always work?" The documentation on the UCase and LCase functions, confirms that it will:

你可能会想,“这总能奏效吗?”有关UCase和LCase功能的文档证实它将:

UCase Function Only lowercase letters are converted to uppercase; all uppercase letters and nonletter characters remain unchanged.

UCase函数只有小写字母转换为大写;所有大写字母和非字母字符保持不变。

LCase Function Only uppercase letters are converted to lowercase; all lowercase letters and nonletter characters remain unchanged.

LCase功能只有大写字母转换为小写字母;所有小写​​字母和非字母字符保持不变。

#3


2  

Private Function IsLetter(Char As String) As Boolean
    IsLetter = UCase(Char) Like "[ABCDEFGHIJKLMNOPQRSTUVWXYZ]"
End Function

#4


1  

What's wrong with the following, which doesn't rely on obscure language behaviour?

以下是什么问题,它不依赖于模糊的语言行为?

Private Function IsLetter(ByVal ch As String) As Boolean
    IsLetter = (ch >= "A" and ch <= "Z") or (ch >= "a" and ch <= "z")
End Function

#5


1  

I believe we can improve upon this a little more. rpetrich's code will work, but perhaps only by luck. The API call's parameter should be a TCHAR (WCHAR here actually) and not a Long. This also means no fiddling with converting to a Long or masking with &HFFFF. This by the way is Integer and adds an implicit conversion to Long here too. Perhaps he meant &HFFFF& in this case?

我相信我们可以对此进行更多改进。 rpetrich的代码可以工作,但也许只能靠运气。 API调用的参数应该是TCHAR(实际上是WCHAR)而不是Long。这也意味着没有摆弄转换为Long或使用&HFFFF屏蔽。顺便说一句,这是Integer并在这里添加一个隐式转换为Long。在这种情况下,也许他的意思是&HFFFF?

On top of that it might be best to explictly call the UnicoWS wrapper for this API call, for Win9X compatibility. The UnicoWS.dll may need to be deployed but at least we gain that option. Then again maybe from VB6 this is automagically redirected, I don't have Win9X installed to test it.

最重要的是,最好为这个API调用明确地调用UnicoWS包装器,以实现Win9X兼容性。可能需要部署UnicoWS.dll,但至少我们获得了该选项。然后再次从VB6可以自动重定向,我没有安装Win9X来测试它。

Option Explicit

Private Declare Function IsCharAlphaW Lib "unicows" (ByVal WChar As Integer) As Long

Private Function IsLetter(Character As String) As Boolean
    IsLetter = IsCharAlphaW(AscW(Character))
End Function

Private Sub Main()
    MsgBox IsLetter("^")
    MsgBox IsLetter("A")
    MsgBox IsLetter(ChrW$(&H34F))
    MsgBox IsLetter(ChrW$(&HFEF0))
    MsgBox IsLetter(ChrW$(&HFEFC))
End Sub

#6


0  

Looking around a bit came up with the following...

看了一下有点以下......

Private Declare Function IsCharAlphaA Lib "user32" Alias "IsCharAlphaA" (ByVal cChar As Byte) As Long

I believe IsCharAlphaA tests ANSI character sets and IsCharAlpha tests ASCII. I may be wrong.

我相信IsCharAlphaA测试ANSI字符集和IsCharAlpha测试ASCII。我可能错了。

#7


0  

Private Function IsAlpha(ByVal vChar As String) As Boolean
  Const letters$ = "abcdefghijklmnopqrstuvwxyz"

  If InStr(1, letters, LCase$(vChar)) > 0 Then IsAlpha = True
End Function

#8


0  

I use this in VBA

我在VBA中使用它

Function IsLettersOnly(Value As String) As Boolean
   IsLettersOnly = Len(Value) > 0 And Not UCase(Value) Like "*[!A-Z]*"
End Function

#9


-1  

It doesn't exactly document itself. And it may be slow. It's a clever hack, but that's all it is. I'd be tempted to be more obvious in my checking. Either use regex's or write a more obvious test.

它并不完全记录自己。而且可能很慢。这是一个聪明的黑客,但就是这样。在我的检查中,我很想明白。要么使用正则表达式,要么写一个更明显的测试。

public bool IsAlpha(String strToCheck)
{
    Regex objAlphaPattern=new Regex("[^a-zA-Z]");
    return !objAlphaPattern.IsMatch(strToCheck);
}

public bool IsCharAlpha(char chToCheck)
{
    return ((chToCheck=>'a') and (chToCheck<='z')) or ((chToCheck=>'A') and (chToCheck<='Z'))
}