473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Need help on vb.net strString.Substring( )

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
2 6395
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Nikita A. Visnevski | last post by:
Hi everyone, I am rather new to Java beans. Just picked up a book last night and started reading about them. I am building an application that allows a user to define objects with dynamic...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
2
by: m.k.ball | last post by:
Thanks Rich - that's great. Before I found this group, I thought I had a reasonable understanding of SQL (well, MySQL's implementation of it, at least) but the truth is there are great chunks that...
2
by: manmit.walia | last post by:
Hello All and Thank You for your time, I am stuck on this exception handleing error. The problem is that when I run my application the application works perfect but sometimes, I get this error. I...
19
by: ash | last post by:
hi friends, i have some questions whch is in my last year question papers.i need some help to get logic of these questions. 1) write a C function, that takes two strings as arguments and...
1
by: sommarlov | last post by:
Hi everyone >From one of our systems an xml file is produced. I need to validate this file before we send it to an external system for a very lenghty process. I cannot change the xml file layout....
10
by: Learner | last post by:
Hello, I am trying to create few dynamic controls and once they are rendered I need to save the information that was entered into these dynamic fileds. For instance when I create 3 radio button...
1
by: donradii | last post by:
Hello all, I am new here and am new the world of javascripting. I am having a heck of a time trying to accomplish something that i conceptually know what i want to do but can not seem to get it...
4
by: Jonathan Wood | last post by:
I'm trying to duplicate an HTML sample I have using my ASP.NET pages. The sample contains the following within the <headtag: <script type="text/javascript" src="flashobject.js"></script>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.