Parsing Strings

photo123

Member
Joined
Feb 11, 2020
Messages
18
Programming Experience
Beginner
I'm trying to parse an entered email address into a text box. I want to split the entered input like this (before the @ sign and after it) BOB | @ | GMAIL.COM. This is what I have so far for my code:
txtEmail.Text = txtEmail.Trim(); // using this to trim the string to make sure there are no spaces
txtEmail = txtEmail.Split(@); //telling it where to split the string entered
This code probably makes no sense but I'm having trouble with this so I try to put together whatever things I found useful from examples I found online.
I also read this link: How to parse strings using String.Split (C# Guide)
I'm still confused on what exactly to write, does anyone have a simpler example of what parsing does and how to do? (I'm a beginner in C# so even if you have the most basic example it'll really help me out)
Thanks!
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,280
Location
Sydney, Australia
Programming Experience
10+
First, what type is txtEmail? The name and the fact that you use its Text property suggests that it is a TextBox? Think about how you actually use a TextBox. Does it have a Trim method? What type actually does have a Trim method? How do you get an object of that type from a TextBox? This is the way you need to think. Go step by step and ask yourself what you're actually trying to achieve, then break that down into smaller steps. Etc.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,152
Location
Chesapeake, VA
Programming Experience
10+
If you know what you will be parsing is an email address, then it's better to use the MailAddress class to do the hard work for you. Pass in the email address to be parsed into the constructor, and then read the User and Host properties to get the parts that you want.

Before you go: "That's an awfully lot of machinery just to figure out what is before and after the '@' symbol. Split() is easier.", take time to read the full RFC5322 section 3.4 (or a little dated, but much easier to read RFC3696 section 3). A quick summary is that people can put the '@' symbol in the user part of the email address and give you input that looks like:
C#:
Abc\@def@example.com
"Abc@def"@example.com
John Doe <john.doe@example.com>

Furthermore, an email address may have a display name and the actual address:
C#:
John Doe <john.doe@example.com>
 

photo123

Member
Joined
Feb 11, 2020
Messages
18
Programming Experience
Beginner
First, what type is txtEmail? The name and the fact that you use its Text property suggests that it is a TextBox? Think about how you actually use a TextBox. Does it have a Trim method? What type actually does have a Trim method? How do you get an object of that type from a TextBox? This is the way you need to think. Go step by step and ask yourself what you're actually trying to achieve, then break that down into smaller steps. Etc.
txtEmail is the name of my textbox. I know that text boxes are of type string by default. The trim method takes put spaces because the example in my book has “Sam Smith. “ and then when they used trim and commented what the string would look like after using it, the space was not there and it became “Sam Smith.” To get the text from a text box you write (name of text box) . Text;
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,152
Location
Chesapeake, VA
Programming Experience
10+
I know that text boxes are of type string by default.
You need to rethink that. Text boxes are of type TextBox. Text boxes are one of many Windows Forms controls. This particular control is used to display strings, and allows the user to edit strings.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,152
Location
Chesapeake, VA
Programming Experience
10+
No Textboxes are Windows Form controls. A variable of the type TextBox will only accept references to instances of TextBox and derived classes of TextBox. Ex:
C#:
TextBox txtLastName = new TextBox();
TextBox txtEmail = new TextBox();
DataGridTextBox dgtbCellEntry = new DataGridTextBox();

txtEmail = txtLastName;
txtEmail = dgtbCellEntry;

This will fail:
C#:
string email = "john.doe@example.com";
TextBox txtEmail = new TextBox();
txtEmail = email;
 

photo123

Member
Joined
Feb 11, 2020
Messages
18
Programming Experience
Beginner
No Textboxes are Windows Form controls. A variable of the type TextBox will only accept references to instances of TextBox and derived classes of TextBox. Ex:
C#:
TextBox txtLastName = new TextBox();
TextBox txtEmail = new TextBox();
DataGridTextBox dgtbCellEntry = new DataGridTextBox();

txtEmail = txtLastName;
txtEmail = dgtbCellEntry;

This will fail:
C#:
string email = "john.doe@example.com";
TextBox txtEmail = new TextBox();
txtEmail = email;
I'm still having trouble with this after reading about this split and trim I'm still not understanding it. This is what I have so far:
String email = txtEmail.Text;
if (txtEmail.Contains("@"){
txtEmail.Text = txtEmail.Split("@");
Could someone please help me out?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,152
Location
Chesapeake, VA
Programming Experience
10+
txtEmail is a Textbox window control. email is a string. The methods Contains() and Split() only exist on strings, not Textbox controls. The way you get a copy of the string that is being displayed by the Textbox control is to access the Text property.
 

Sheepings

Retired Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,828
Location
UK
Programming Experience
10+
Throw a dog a bone Skydiver ;)

C#:
            string email_str = "bob101@gmail.com"; /* This is a string, and it has the same characteristics of a .text property of a textbox control */
            string[] str_arr = email_str.Split('@'); /* This is a string array, it too is a variable capable of storing multiple strings. .Split('') returns a string array of strings. */
            textBox1.Text = str_arr[0]; /* This equals the first occurrence of the first variable in the string array. */
            textBox2.Text = email_str.Substring(email_str.IndexOf("@"), 1); /* .Substring() creates a second string variable from the integral point of where 
"@" exists in the string, and we do that by allowing IndexOf() to be the reference provider to provide the integral point of where "@" is in this string, 
and 1 takes only this value and returns the substring to the Textbox .Text property as "@". */
            textBox3.Text = str_arr[1]; /* This equals the second occurrence, just as explained for the first occurrence. */

To learn about these methods. Read the links in my signature under the spoiler.
 

photo123

Member
Joined
Feb 11, 2020
Messages
18
Programming Experience
Beginner
Throw a dog a bone Skydiver ;)

C#:
            string email_str = "bob101@gmail.com"; /* This is a string, and it has the same characteristics of a .text property of a textbox control */
            string[] str_arr = email_str.Split('@'); /* This is a string array, it too is a variable capable of storing multiple strings. .Split('') returns a string array of strings. */
            textBox1.Text = str_arr[0]; /* This equals the first occurrence of the first variable in the string array. */
            textBox2.Text = email_str.Substring(email_str.IndexOf("@"), 1); /* .Substring() creates a second string variable from the integral point of where
"@" exists in the string, and we do that by allowing IndexOf() to be the reference provider to provide the integral point of where "@" is in this string,
and 1 takes only this value and returns the substring to the Textbox .Text property as "@". */
            textBox3.Text = str_arr[1]; /* This equals the second occurrence, just as explained for the first occurrence. */

To learn about these methods. Read the links in my signature under the spoiler.
Thanks Sheepings and Skydiver for your help!
 
Top Bottom