I have an assignment that asks me to do the following things:

1. Create a textbox to get the student's first and last name (ie: John Ark)

2. If the student's "first letter of his last name" (which is "A" in John Ark) ranged from A to I, then puts him in Group 1.
From J to S: Group 2
From T to Z: Group 3

3. Display the answer (ie: "John Ark is in Group 1")

Here's my code (I just want to test the first Group (A-I)):

Dim strname As String = Me.txtenter.Text
        Dim strPattern1 As String
        Dim intpos As Integer
        intpos = strname.IndexOf(" ")
        Dim strnew As String
        strnew = strname.Substring(Val(intpos), 2) 'Locate the last name's first letter
        strPattern1 = "[A-I]"
        Me.lblanswer.Text = strnew  Like strPattern1

I enter "John Ark", the his last name's first letter is "A", which lies exactly in Group 1. Thus, the "Like operator" is supposed to say "True". But the problem is: it keeps saying "False".

Can anybody tell me what is wrong with my code, please?

7 Years
Discussion Span
Last Post by apegram

Look at the following line:

intpos = strname.IndexOf(" ")

Then you use that value for your substring function

strnew = strname.Substring(Val(intpos), 2) 'Locate the last name's first letter

Do you see what you are doing there? Outside of making an unnecessary Val call (Substring's first parameter is an integer, and intpos is already an integer), you are using intpos as the beginning index for your substring. You aren't pulling the "Ar" of John Ark, you are getting " A". Your first character is the space!

strnew = strname.Substring(intpos + 1, 1) 'Locate the last name's first letter

That will do what you intend.

Edited by apegram: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.