Need help on vb.net strString.Substring( )

M

mallard134

Could someone please help a newbee vb programmer with a question that
is driving me crazy. I am trying to understand a line of code that is
supposed to return the domain portion of a valid email address. The
following code works!

******************************************************************

strEmail = "(e-mail address removed)"

String2 = strEmail.Substring(strEmail.IndexOf("@") + 1, _
strEmail.IndexOf(".", strEmail.IndexOf("@") + 1) _
- strEmail.IndexOf("@") - 1)

******************************************************************

String2 will contain the string "mydomain" . I understand the IndexOf(
) method
will return a zero based index of the char your searching for and that
the method
string.Substring( ) returns a substring of a string using start and
size arguments,
ie: Substring( start, size) but if you look at the email above the
start position would be the
index of 5 or the 'm' in mydomain. the num chars would be 13 - 3 = 10
chars. So if you
start at the 5th char and return 10 char's I would think that you would
return the string
"mydomain.c". Now to really get things confusing I did a little test
in the following code.

I broke up the different index search's only I took the index of the
period + 1 and subtracted
the position of the '@' sign + 1 which made more sense to me and I got
8 char's, then I used that value in the substring( ) procedure and I
get the correct answer. My confusion is how can both way's work?

Obviously I am really confused on this, if anyone could help with the
explanation I would really appreciate it.

Thanks

Scooby

**************************************************************

Dim strEmail As String
Dim var1 As Integer
Dim var2 As Integer
Dim var3 As Integer
Dim String1 As String
Dim String2 As String

strEmail = "(e-mail address removed)"

var1 = strEmail.IndexOf("@") + 1
var2 = strEmail.IndexOf(".", strEmail.IndexOf("@") + 1)
var3 = var2 - var1

String1 = strEmail.Substring(strEmail.IndexOf("@") + 1, var3)

String2 = strEmail.Substring(strEmail.IndexOf("@") + 1, _
strEmail.IndexOf(".", strEmail.IndexOf("@") + 1) _
- strEmail.IndexOf("@") - 1)

Both String1 and String2 will = "mydomain"?
 
M

mallard134

Mattias:

Thanks for the response, as soon as you typed the simple line, 12
- 4 - 1 = 8 I could see what I was missing. In the last IndexOf( ) line
"strEmail.IndexOf("@") - 1)" I took that line as "take the index of the
( @ sign minus 1) ie: in (e-mail address removed) that would be 4-1 = 3 ) and
subtract from the index of the period" What I missed was that it's
"Subtract the index of the @ sign from the index of the period, and
then subtract one more". Man I can't believe I was that hung up on
something so simple. ugh!

Thanks a ton for you help :)

Scooby.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top