Validate email address format in .NET without exceptions [duplicate] - c#

This question already has answers here:
Check that email address is valid for System.Net.Mail.MailAddress
(6 answers)
Best Regular Expression for Email Validation in C#
(6 answers)
Closed 5 years ago.
Currently I'm using this code:
public static bool IsEmailAddress(this string value)
{
try
{
new System.Net.Mail.MailAddress(value);
return true;
}
catch ()
{
return false;
}
}
Is there a way to do this without using exceptions?

You could use a regular expression.
private const string EmailRegularExpression = #"^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$";
private static Regex EmailValidRegex = new Regex(CommonValues.EmailRegularExpression, RegexOptions.Compiled | RegexOptions.IgnoreCase)
public static bool IsEmailValid(string emailAddress)
{
return !string.IsNullOrWhiteSpace(emailAddress) && EmailValidRegex.IsMatch(emailAddress);
}
See also http://www.regular-expressions.info/email.html which is where I snagged the expression from.

Related

Use # symbol in regex search as keyword in C# [duplicate]

This question already has answers here:
Regex to match a word with + (plus) signs
(5 answers)
Search and replace method using regular expressions
(1 answer)
Closed 3 months ago.
Created an extension method and it works for normal words but When I added an # as part of search keyword it stopped working. Can someone help?
public static class Extensions
{
public static string ReplaceWholeWord(this string inputString, string oldValue, string newValue)
{
string pattern = #"\b#test\b"; // #"\b$\b".Replace("$",oldValue);
return Regex.Replace(inputString, pattern, newValue);
}
}
static void Main(string[] args)
{
string input = "#test, and #test but not testing. But yes to #test";
input = input.ReplaceWholeWord("#test", "text");
Console.WriteLine(input);
}

Finding a string within another string using C# [duplicate]

This question already has answers here:
Find text in string with C#
(17 answers)
Closed 3 years ago.
How do I find out if one of my strings occurs in the other in C#?
For example, there are 2 strings:
string 1= "The red umbrella";
string 2= "red";
How would I find out if string 2 appears in string 1 or not?
you can use String Contains I guess. pretty sure there is similar question have been asked before How can I check if a string exists in another string
example :
if (stringValue.Contains(anotherStringValue))
{
// Do Something //
}
You can do it like this:
public static bool CheckString(string str1, string str2)
{
if (str1.Contains(str2))
{
return true;
}
return false;
}
Call the function:
CheckString("The red umbrella", "red") => true

editing injected parameter without returning value c# [duplicate]

This question already has answers here:
When to use in vs ref vs out
(17 answers)
Closed 6 years ago.
I have a question: is it possible to edit the entry of the function without returning any value and those entries will be edited?
Idea:
void AddGreeting(string value)
{
value = "Hi " +value;
}
and calling this function like this:
string test = "John";
AddGreeting(test);
//and here the test will be "Hi John"
is that possible? and how to do it if it is?
You could easily use the ref parameter as so:
void AddGreeting(ref string value){}
and this would do what you want:
void AddGreeting(ref string value)
{
value = "Hi " +value;
}
string test = "John";
AddGreeting(ref test);
Alternatively, you could return a string, which i would consider neater and cleaner to look at

How to "invert" the value of a string? [duplicate]

This question already has answers here:
Best way to reverse a string
(51 answers)
Closed 6 years ago.
In C# want to do something like this:
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("123");
And then some code
//Outputs 321 (works for any text value)
In C#
public string Reverse(string s) {
return new string(Array.Reverse(s.ToCharArray()));
}
From above 3.5 Framework:
public string Reverse(string s) {
return new string(s.Reverse().ToArray());
}
See reference.

How can I convert string to Guid? [duplicate]

This question already has answers here:
Test if string is a guid without throwing exceptions?
(19 answers)
Closed 5 years ago.
I have string type value like "e2ddfa02610e48e983824b23ac955632". I need to add - in this code means convert in Guid.
EntityKey = "e2ddfa02610e48e983824b23ac955632";
Id = (Guid)paymentRecord.EntityKey;
Just a simple creation:
String source = "e2ddfa02610e48e983824b23ac955632";
Guid result = new Guid(source);
You could do :
Guid guid;
if (Guid.TryParse("e2ddfa02610e48e983824b23ac955632", out guid))
{
// succeed...
}
else
{
// failed...
}
Edit : Like #Silvermind said, if you know the format of the input, you can use Guid.TryParseExact with the "N" format in your case.
For parsing the string to a Guid. You can do this:
var guid= "e2ddfa02610e48e983824b23ac955632";
var result= Guid.ParseExact(guid,"N")
Or if you prefer to have it in a try parse. You can also do this:
Guid result;
if(Guid.TryParseExact(guid,"N",out result))
{
//Do something
}
The "N" is a format which indicates that the string will be format with 32 digits without "-"
Reference:
Guid.TryParseExact Method (String, String, Guid)

Categories