Sam Apostel
Sam Apostel

Reputation: 603

How to test string for only letters or apostrophes?

I need to check a string for a few things:

  1. the first letter has to be a capital letter
  2. only letters or apostrophes (')
  3. a max of 2 apostrophes can be used

The code below always returns that the string doesn't conform except when I input just 1 capital letter. How can I correct it to fulfil my requirements?

Private Function setName(ByVal pName As String)
    Dim letters As Integer()
    Dim aphCount As Integer = 0
    Dim isvalid As Boolean = True
    For i As Integer = 0 To pName.Length - 1 Step 1
        ReDim letters(i)
        letters(i) = Asc(pName.Substring(i, 1))
    Next

    If Not letters(0) >= 65 And letters(0) <= 90 Then
        isvalid = False
    End If

    For i As Integer = 1 To pName.Length - 1 Step 1
        If letters(i) >= 39 And letters(i) <= 122 Then
            If letters(i) = 39 Then
                aphCount += 1
                If aphCount > 2 Then
                    isvalid = False
                End If
            ElseIf letters(i) >= 65 And letters(i) <= 90 Then

            ElseIf letters(i) >= 97 And letters(i) <= 122 Then

            Else isvalid = False
            End If
        Else isvalid = False
        End If
    Next

    If isvalid = False Then
        If MsgBox("you put in an invalid name", MsgBoxStyle.RetryCancel, "name error") = MsgBoxResult.Cancel Then
            pName = "Hero" & heroCount
        Else
            pName = inputName()
            pName = setName(pName)
        End If
    End If
    Return pName
End Function

edit: thank you all for helping, I got to learning regex and came up with something that works (using java, I switched to java because this is a project for me to learn coding, java has more to offer):

public void checkName(String name)  throws IllegalArgumentException{
     String noSpaceName = name.replaceAll("\\s+","");
     String pattern = "^[A-Z][A-Za-z]*'?[A-Za-z]*'?[A-Za-z]*";    
     Pattern re = Pattern.compile(pattern);
     Matcher m = re.matcher(noSpaceName);
     if (m.matches()){
         name.replaceAll("\\s+"," ");
         super.setName(name);
     }else throw new IllegalArgumentException ("Exception: Name is invalid");
}

Upvotes: 0

Views: 321

Answers (2)

Enigmativity
Enigmativity

Reputation: 117134

You can do this with regular expressions in two lines.

Private Function IsValidName(name As String) As Boolean
    Dim match = Regex.Match(name, "^[A-Z]((')|[A-Za-z])*$")
    Return match.Success AndAlso match.Groups(2).Captures.Count <= 2
End Function

Upvotes: 1

Saragis
Saragis

Reputation: 1792

Your code is very complex for what you are trying to do. It's pretty long and it contains a lot of magic numbers. You can solve this either with regular expressions or by regular comparing of each element in the string. This is an example of the second way:

Private Function IsValidName(name As String) As Boolean
    If String.IsNullOrWhiteSpace(name) Then Return False

    Return Char.IsUpper(name.AsEnumerable().First()) AndAlso
           name.AsEnumerable().All(Function(c) Char.IsLetter(c) OrElse c.Equals("'"c)) AndAlso
           name.AsEnumerable().Count(Function(c) c.Equals("'"c)) <= 2
End Function

Upvotes: 1

Related Questions