Hello dear c#/java lovers ,well this article is tailored for the c# lovers but Java devs can possibly pick a lot from it. Anyway let’s get on with it yeah? Walk with me.
So I was in my cozy dark room coding away and skimming through the internet on how to find/locate a substring at the beginning or end of a short or ridiculously long string like("Hubert Blaine Wolfeschlegelsteinhausenbergerdorff") 🤣, fortunately for me I came across a very interesting method (thank you Microsoft) which goes by the name of “EndsWith()”, basically what the EndsWith() method does is that it checks a string (ridiculously long or not) to find a substring at the end of a string, so it got me thinking about a random problem/kata 😊 .
I wanted to write a mini console log application to do a mail Validation check for any type of e-mail service under the sun (if you are planning on building one, good luck, I am rooting for you) using the “EndsWith()” and well I did 😊 . As we all know we have popular email providers such as Gmail, outlook , yahoo mail, Hotmail, so I focused on just these ones for this mini application.
Yeah so first off I will be creating a “validateEmail()” function and passing in a parameter of “enterEmail” with its data type as “string” as you can see below.
Next we declare our variables for each mailing service we want to check for, in our case we will be checking for gmail, yahoo mail, outlook mail and hotmail, and also a message to display if the email address entered is valid or not.
Now this is the exciting part, where we get to see EndsWith() in action,
Awesome right? But we are not done, just one last thing left to do, call the function , because what is the purpose of a function if it is not called 😊 . Lets go ahead to call the validateEmail() function and receive an input which will serve as the argument passed for "enterEmail", we can enter any email format to check if it is really validating.
Notice there that the email format entered is “tobby@mail” which obviously is not a valid email address so it returns a message “Your mailing address is not valid” but using "tobby@gmail.com" returns a message “your mailing address is valid”.
So there we have it folks, the power of the EndsWith() method, you can do much more than this, if you enjoyed the article kindly leave a comment 👍.
Gracias.