By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,925 Members | 732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,925 IT Pros & Developers. It's quick & easy.

Need help on vb.net strString.Substring( )

P: n/a
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 = "j...@mydomain.com"

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 = "j...@mydomain.com"

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"?

Nov 24 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
the num chars would be 13 - 3 = 10 chars.


Where did you get 3 from? I get 13 - 4 - 1 = 8.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 24 '05 #2

P: n/a
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 j...@mydomain.com 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.

Nov 25 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.