VB.NET how can I check if String Contains alphabet characters and .?

100,230

Solution 1

The logic is a little off in the highest rated answer. The function will only validate the first character and then return true. Here's the tweak that would validate the entire string for alpha characters:

'Validates a string of alpha characters
Function CheckForAlphaCharacters(ByVal StringToCheck As String)
    For i = 0 To StringToCheck.Length - 1
        If Not Char.IsLetter(StringToCheck.Chars(i)) Then
            Return False
        End If
    Next

    Return True 'Return true if all elements are characters
End Function

Solution 2

Use this function:

Function CheckForAlphaCharacters(ByVal StringToCheck As String)


    For i = 0 To StringToCheck.Length - 1
        If Char.IsLetter(StringToCheck.Chars(i)) Then
            Return True
        End If
    Next

    Return False

End Function

Usage

    Dim Mystring As String = "abc123"
    If CheckForAlphaCharacters(Mystring) Then
        'do stuff here if it contains letters
    Else
        'do stuff here if it doesn't contain letters
    End If

Solution 3

I believe I have another way you could check if there was a letter in a string, if thats all you wanted to check. say you have a random string of characters. example "12312H231" the string isnt the same as this because it has a letter in it "12312h231" if you use string.tolower = string.toupper. it will be false if it has a letter, and true if it has numbers only.

if string1.toupper = string1.tolower then
'string1 is a number
else
'string1 contains a letter
end if

Solution 4

Apparently I cannot vote up or comment here. However, the RegEx answer is by far the simplest and best solution to this problem. For example, if you need to test a string as to whether it is numeric or alpa or alphanumeric or a mix, whatever, this solution accomplishes the task.

Test if a string is numeric use "[0-9]*" Alpha only All Upper Case "[A-Z]*" Alpha only, Mix Case Allowed: "[A-Z][a-z]*" Two digits, One upper case letter, then six lower case letters followed by a single digit that is a '3' or a '7': "[0-9]{2}[A-Z][a-z]{6}[3,7]"

For example, this statement tests whether a string begins with two letters and ends with 7 digits: (It is taken from SSRS)

System.Text.RegularExpressions.Regex.ismatch(Fields!YourStringToTest.Value,"[A-Z][a-z]{2}[0-9]{7}")

Solution 5

I added a check for whitespace as I am looking at names. This will not return false if there is a space in your string.

    'Validates a string of alpha characters
    Function CheckForAlphaCharacters(ByVal StringToCheck As String)
        For i = 0 To StringToCheck.Length - 1
            If Not Char.IsLetter(StringToCheck.Chars(i)) Then
                If Not Char.IsWhiteSpace(StringToCheck.Chars(i)) Then
                    Return False
                End If
            End If
        Next

        Return True 'Return true if all elements are characters

End Function

Share:
100,230
user840930
Author by

user840930

Updated on July 09, 2022

Comments

  • user840930
    user840930 almost 2 years

    Using visual basic, I have a string. Want to check that the string contains a single, capital alphabetic character followed by a period. Tried to use Contains as in the following:

    someString.Contains("[A-Z].") but this didn't return me what I wanted.

    Also need to check for a single number followed by a period.

    How can I do this in Visual Basic

  • user840930
    user840930 about 12 years
    how do I create a regex in Visual Basic?
  • Brian
    Brian about 12 years
    just put the regex string in double quotes. Regex is part of the System.Text.RegularExpression namespace. It takes in two values. the input string and a string of the regex.
  • user840930
    user840930 about 12 years
    I tried: Dim rowHeaderRegex As Regex = New Regex("([A-Z]|.|\?)") Dim M As Match = rowHeaderRegex.Match(rowName) It worked for "A. SomeString" but "1. SomeString" also returned success does it need to be different to exclude numbers?
  • shadowspawn
    shadowspawn almost 5 years
    This does not answer the question which was asked.
  • Peter Duniho
    Peter Duniho almost 5 years
    @shadowspawn: I agree. Though, it turns out that none of the previously posted answers do, strictly speaking. Most provide enough information for the OP to get to where they needed to be, but I don't feel that any of the actually answered the question. I've made an attempt to fix that. :)
  • Peter Duniho
    Peter Duniho almost 4 years
    @Poyda: "is that not ASCII?" -- no, the letter 'A' is not ASCII. In C#, it's UTF16, and in any case the character point itself can be encoded in any character encoding. Saying the character itself is any particular encoding is simply wrong. In any case, the question does not stipulate 'A' through 'Z'...that just happens to be the characters they used in their failed attempt to solve the problem. That's not the specification...the specification can be found in the question text before the code example. As far as whether this answer is "excellent" or not, well...
  • Peter Duniho
    Peter Duniho almost 4 years
    ... I obviously feel it's a well-written answer and the only one that actually addresses the question that was asked in precise terms. But clearly no one else, including you, has found it useful, never mind "excellent", your comment notwithstanding.