VB.NET, how can I check if String contains alphabet and. Characters?

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

someString.Contains ("[AZ]."), but this does not return to me what I wanted.

It is also necessary to check one number followed by a period.

How to do it in Visual Basic

+6
source share
11 answers

The logic is slightly different from the highest rating. The function will only check the first character, and then return true. Here's a setting that will check 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 
+11
source

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 

Using

  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 
+10
source

I believe that I have another way to check if there is a letter in the line, if that's all you wanted to check. let's say you have a random string of characters. For example, the “12312H231” string is not the same as this, because it has the letter “12312h231” if you use string.tolower = string.toupper. it will be false if it has a letter, and true if it has only numbers.

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

I have a simple solution. But the decision is not elegant. You can first create a string, that is, from "A." , "B.", before "Z.".

So, suppose the variable MyString contains the string you want to test. Then you can check MyString for "A." string using the String.IndexOf method. If the return value is non-negative, then "A." exists in MyString. Continue for "B.", "C." to "Z.". You can repeat the same path for an integer, that is, "0.", "1." to "9."

+3
source

One solution is to use regular expressions. You can check the following regular expression according to the alphabets. and?

 ([AZ]|.|\?) 

I have not tested the above.

+2
source

I have added a check for spaces as I am looking for names. This will not return false if there is a space in your line.

  '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 

Final function

+2
source

Apparently, I can not vote or comment here. However, RegEx's answer is by far the easiest and best solution to this problem. For example, if you need to check a string as to whether it is numeric or alp or alphanumeric or mix, whatever that is, this solution performs the task.

Check if the numeric string "[0-9]*" Alpha only All upper cases "[AZ]*" Alpha only, allowable distance: "[AZ][az]*" Two digits, one uppercase letter, then six lowercase letters followed by a single digit that is “3” or “7”: "[0-9]{2}[AZ][az]{6}[3,7]"

For example, this statement checks if a string starts with two letters and ends with 7 digits: (taken from SSRS)

 System.Text.RegularExpressions.Regex.ismatch(Fields!YourStringToTest.Value,"[AZ][az]{2}[0-9]{7}") 
+2
source

You can create a regex to test this (or google for a regex that does what you need. Then call regex.match

 Regex.Match(inputString, "regex") 
+1
source

FYI, In VBA we need to use Lcase for the lower character and Ucase for the upper character

 If Lcase(String1) = Ucase(String2) then Msgbox " Numbers" Else Msgbox " Alphabets" End if 
+1
source

I am upset to find that this question, which seems to be written quite clearly, if not well researched, has ten unanswered answers, but none of which actually exactly answers the question, not even the accepted answer.

The question is here:

make sure the string contains one uppercase alphabetical character followed by a period. ... It is also necessary to check for the presence of one number followed by a period.

This answer is closest, but it has two drawbacks: firstly, it includes a "?" a character for some reason, even if the question was not asked by this, but more problematic, like most other answers, it incorrectly assumes that the input is strictly in the ASCII range, which will not work even for accented Latin characters, not to mention characters from other scenarios.

The accepted answer, along with two others, checks that all the characters in the input are letters, and this is not what they asked for, and then also does not check the upper- case, which asks the question.

It is inexplicable to me that so many incorrect answers could be posted without a single correct answer. But I intend to fix it.

Then there are five different approaches to the problem (code below) ...


The first two suggest that you can use any Unicode letter, which is the upper- register, as well as any Unicode digit, that the character following it must be "." character, and that a string of any length satisfying these conditions meets the requirements. Note that there are “decimal digits” in several different scenarios, such as Arabic-Indian, Devengari, Bengali, etc. Similarly, scripts from around the world have the idea of ​​upper- and lowercase letters, including non-Latin scripts such as Cyrillic, Greek, etc.

A properly localized solution should take all this into account and work regardless of the language (and, therefore, the script) that the program uses.

In these first two solutions, there is one that scans the string and explicitly validates each character, while the other uses a regular expression to perform validation. The first may be easier to understand, but IMHO regular expression is more expressive and not so complicated.

However, sometimes you really want to limit the behavior of the program to support only Latin scripts. Therefore, solutions from the third to the fifth I propose. The third is similar to the very first solution, but allows only Latin characters for the upper- and numeric letters. The fourth and fifth are two different approaches based on regular expressions: the fourth is a little easier to write, but requires an explicit range of character values ​​for Latin characters; the fifth uses Unicode character block names and allows the Regex class to determine the actual character values.

The fifth is more verbose, but in this case I find it more expressive, in the sense that you can say why it was these ranges of symbol blocks that were chosen. It is also much easier to change the restriction. For example, if you only need a basic Latin character set, without accented characters, side characters, etc., you can simply include the value "IsBasicLatin" .

Note that both of these regex-based solutions require the use of “zero-width look-ahead” in order to accomplish what essentially is a “and” comparison. Those. The preview first checks that the character is in the correct range of characters in the script, and then the main part of the regular expression then actually checks the case or upper- digits.

This sample code also includes sample data and a simple helper method for testing each implementation. Please note that in this sample, the second and third row copies, which appear to have capital of Latin character, actually have Cyrillic capital A character. Thus, when testing for compliance with the requirements of Latin characters, the test did not pass, and when testing on a globalization-friendly implementation that supports all scenarios, it will pass.

Sample data also includes a line with an indicator number ١ . Again, this will pass a test friendly to globalization, but will fail only in Latin.

Hope this helps!

 Imports System.Text Imports System.Text.RegularExpressions Module Module1 Sub Main() Console.OutputEncoding = Encoding.UTF8 Dim values As String() = {"A.", "0.", "aZ.foo", "b9.bar", "É.", ".", "!", "e.", "a١.foo", "b0,bar", "é."} TestValidateWithValues(values, AddressOf ValidateString) TestValidateWithValues(values, AddressOf ValidateStringRegex) TestValidateWithValues(values, AddressOf ValidateLatinString) TestValidateWithValues(values, AddressOf ValidateLatinStringShortRegex) TestValidateWithValues(values, AddressOf ValidateLatinStringVerboseRegex) End Sub Function ValidateString(text As String) As Boolean If text.Length < 2 Then Return False For i As Integer = 0 To text.Length - 2 Dim ch As Char = text(i) If (Char.IsLetter(ch) And Char.IsUpper(ch) Or Char.IsDigit(ch)) And text(i + 1) = "."c Then Return True Next i Return False End Function Function ValidateStringRegex(text As String) As Boolean Return Regex.IsMatch(text, "(?:\p{Lu}|\p{Nd})\.") End Function Function ValidateLatinString(text As String) As Boolean If text.Length < 2 Then Return False For i As Integer = 0 To text.Length - 2 Dim ch As Char = text(i) If (IsLatinChar(ch) And Char.IsUpper(ch) Or IsLatinDecimalDigit(ch)) And text(i + 1) = "."c Then Return True Next i Return False End Function Function IsLatinChar(ch As Char) As Boolean Return ch >= ChrW(0) And ch <= "ʯ"c ' IsBasicLatin, IsLatin-1Supplement, IsLatinExtended-A, IsLatinExtended-B, IsIPAExtensions End Function Function IsLatinDecimalDigit(ch As Char) As Boolean Return ch >= "0"c And ch <= "9"c End Function Function ValidateLatinStringShortRegex(text As String) As Boolean Return Regex.IsMatch(text, "(?=[\u0000-ʯ])(?:\p{Lu}|\p{Nd})\.") End Function Function ValidateLatinStringVerboseRegex(text As String) As Boolean Return Regex.IsMatch(text, "(?=[\p{IsBasicLatin}\p{IsLatin-1Supplement}\p{IsLatinExtended-A}\p{IsLatinExtended-B}\p{IsIPAExtensions}])(?:\p{Lu}|\p{Nd})\.") End Function Sub TestValidateWithValues(values As String(), validator As Func(Of String, Boolean)) Console.WriteLine($"Testing method {validator.Method.Name}:") For Each value In values Console.WriteLine($"Text: {value}, Result: {validator(value)}") Next Console.WriteLine() End Sub End Module 

ps Do you want something in your console window font that supports all tested characters, for example, "Courier New", if you do not want to see a character replacement character instead of the actual character for the ١ character.

0
source

This is an old question, but people still want to know. This is pretty easy. It is important to convey only one character. The function will tell if it is alphanumeric and will give a logical answer.

If the character matches what is in the AlphaNumericValidator variable, then it is alphanumeric. No loops. Pretty simple.

  Public Function Is_AlphaNumeric(Text As String) As Boolean Dim AlphaNumericValidator As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890" Dim b_Result As Boolean If InStr(AlphaNumericValidator, Text) > 0 Then b_Result = True Else b_Result = False End If Return b_Result End Function 
-one
source

Source: https://habr.com/ru/post/1396413/


All Articles