The c# regex.match method is typically used to validate a string or to ensure that a string conforms to a particular pattern without retrieving that string for subsequent manipulation.Below is a string extension method that uses c# regex matches to check if the string is alphanumeric.It will return true if given string contain only number and alphabets.
using System;
using
System.Collections.Generic;
using
System.Text;
using
System.Text.RegularExpressions;
public static class CheckString
{
public static bool
IsAlphanumeric(string source)
{
Regex
pattern = new Regex("[^0-9a-zA-Z]");
return
!pattern.IsMatch(source);
}
}
// THE Below
Example USe Code.
class Program
{
static void Main(string[]
args)
{
string
testString = Console.ReadLine();
if
(CheckString.IsAlphanumeric(testString))
Console.WriteLine("Yes string is Alphanumeric!");
else
Console.WriteLine("No string is not Alphanumeric!");
Console.ReadKey();
}
}
I have implemenetd a common method to clear the Text of few Controls. One of that is Textbox.If sometimes you requires to clear all the input fields of a web page then just call the below c# method this method will clear all textbox values to empty.
public void ClearAllTextBOX(ControlCollection
ctrls)
{
foreach
(Control ctrl in
ctrls)
{
if
(ctrl is TextBox)
((TextBox)ctrl).Text
= string.Empty;
ClearInputs(ctrl.Controls);
}
}
}
Often we need to capitalize the first letters of some word
or some text (for example when we want to display users name or city name etc).
Since string class does not have a method to do this we
could think that there is no built-in solution in C# for this problem.
Here i am giving two solution for this problem.
Solution 1-
We can use
ToTitleCase method of TextInfo class in System.Globalization
namespace for this problem.
public static string
Capitalize(string value)
{
return System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value);
}
Solution 2-
The below method will return Capitalize Words.
public static string
CapitalizeWords(string value)
{
if
(value == null)
throw
new ArgumentNullException("value");
if
(value.Length == 0)
return
value;
StringBuilder
result = new StringBuilder(value);
result[0] = char.ToUpper(result[0]);
for (int i = 1; i < result.Length; ++i)
{
if
(char.IsWhiteSpace(result[i - 1]))
result[i] = char.ToUpper(result[i]);
}
return
result.ToString();
}
When we thinking how to solve this is problem JavaScript comes in our mind And off course,
that IS THE WAY TO GO.
ASP.NET 2.0 introduced DefaultFocus and DefaultButton properties for HtmlForm class that
you can easily use for requirements like this.
DefaultFocus property gets
or sets the child control on the HtmlForm that will receive the focus when the
HtmlForm is loaded.
DefaultButton property gets or sets the child control of the HtmlForm that causes postback when enter key is pressed on the page.
Here is an example on how to use this two
properties in your pages:
<form id="formtest" runat="server" defaultfocus="txtfirstname"
defaultbutton="btnsubmit ">
defaultbutton="btnsubmit ">
<div>
Name:
<asp:TextBox ID="txtfirstname btnsubmit" runat="server"></asp:TextBox><br />
Address:
<asp:TextBox ID="txtadress" runat="server"></asp:TextBox><br />
<asp:Button ID="btnsubmit" runat="server" Text="Submit" />
<asp:Button ID="btnsubmit" runat="server" Text="Cancel" />
</div>
</form>
NOte: In order for this to work, your form must have runat="server" attribute set.
In Implicit type conversion one datatype is automatically converted to other datatype by compliler.
using System;
class ConversionTest
{
public static void Main()
{
byte mybyte = 1;
int myintA = 1234;
int myintB = mybyte; // this will Implicit cast from byte to int datatypes.
double DoubleD = myintA; // this is Implicit cast from int to double datatype.
Console.WriteLine("{0}", myintB);
Console.WriteLine("{0}", DoubleD);
}
}
Explicit type conversion
In explicit type conversion the type conversion is explicitly defined within a program it is not done by compiler Implicitly.
double A = 101.2;
double B = 305.3;
double C = 306.4;
int result = (int)A + (int)B + (int)C; //result == 712
A regular expression known as regex in short is a special text string which describes a search pattern.
Here is a c# function which accept a string as input parameter and return true if string is valid Email address and return false if string is not valid .
For this function to work properly you have to add these namespaces.
using System.Text;
using System.Text.RegularExpressions;
protected bool checkEmail(string Emailtext)
{
Regex MyEmailRegex = new Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
if (string.IsNullOrEmpty(Emailtext))
{
return false;
}
else
{
return MyEmailRegex.IsMatch(Emailtext);
}
}
Structs are the userdefined data type. Structs are defined by using the struct keyword in c#.
for example:
It is an error to define a default (parameterless) constructor for a struct. It is also an error to initialize an instance field in a struct body. You can initialize struct members only by using a parameterized constructor or by accessing the members individually after the struct is declared.
This example demonstrates struct initialization using both default and parameterized constructors.
Structs Almost have same syntax as classes, But structs are more limited than classes:
• Within a struct declaration, fields cannot be initialized unless they are declared as const or static.
• A struct cannot declare a default constructor (a constructor without parameters) or a destructor.
• Structs are copied on assignment. When a struct is assigned to a new variable, all the data is copied, and any modification to the new copy does not change the data for the original copy.
• Structs are value types but classes are reference types.
• structs can be instantiated without using a new operator.
• Structs can declare constructors that have parameters.
• A struct cannot support the inheritance.
• All structs inherit directly from System.ValueType, which inherits from System.Object.
• A struct can implement interfaces.
• A struct can be used as a nullable type and can be assigned a null value.
for example:
public struct StructA
{
public int x, y;
public A(int p1, int p2)
{
x = p1;
y = p2;
}
}
{
public int x, y;
public A(int p1, int p2)
{
x = p1;
y = p2;
}
}
It is an error to define a default (parameterless) constructor for a struct. It is also an error to initialize an instance field in a struct body. You can initialize struct members only by using a parameterized constructor or by accessing the members individually after the struct is declared.
This example demonstrates struct initialization using both default and parameterized constructors.
public struct StructA
{
public int x, y;
public StructA (int p1, int p2)
{
x = p1;
y = p2;
}
}
class TestStructA
{
static void Main()
{
// Initialize:
StructA StructA1 = new StructA();
StructA StructA2 = new StructA(10, 10);
// Display results:
Console.Write("StructA 1: ");
Console.WriteLine("x = {0}, y = {1}", StructA1.x, StructA1.y);
Console.Write("StructA 2: ");
Console.WriteLine("x = {0}, y = {1}", StructA2.x, StructA2.y);
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
{
public int x, y;
public StructA (int p1, int p2)
{
x = p1;
y = p2;
}
}
class TestStructA
{
static void Main()
{
// Initialize:
StructA StructA1 = new StructA();
StructA StructA2 = new StructA(10, 10);
// Display results:
Console.Write("StructA 1: ");
Console.WriteLine("x = {0}, y = {1}", StructA1.x, StructA1.y);
Console.Write("StructA 2: ");
Console.WriteLine("x = {0}, y = {1}", StructA2.x, StructA2.y);
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
Structs Almost have same syntax as classes, But structs are more limited than classes:
• Within a struct declaration, fields cannot be initialized unless they are declared as const or static.
• A struct cannot declare a default constructor (a constructor without parameters) or a destructor.
• Structs are copied on assignment. When a struct is assigned to a new variable, all the data is copied, and any modification to the new copy does not change the data for the original copy.
• Structs are value types but classes are reference types.
• structs can be instantiated without using a new operator.
• Structs can declare constructors that have parameters.
• A struct cannot support the inheritance.
• All structs inherit directly from System.ValueType, which inherits from System.Object.
• A struct can implement interfaces.
• A struct can be used as a nullable type and can be assigned a null value.
