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.