using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str =null;
if (str == null)
{
MessageBox.Show("String is null");
}
else
{
MessageBox.Show("String is not null");
}
str = "test";
if (string.IsNullOrEmpty(str))
{
MessageBox.Show("String is empty or null");
}
else
{
MessageBox.Show("String is not empty or null");
}
}
}
}
Showing posts with label c sharp string operations. Show all posts
Showing posts with label c sharp string operations. Show all posts
Tuesday, February 28, 2017
How to C# String Null
How to use C# string Copy
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str1 = null;
string str2 = null;
str1 = "CSharp Copy() test";
str2 = string.Copy(str1);
MessageBox.Show(str2);
}
}
}
When you run this C# source code you will get same content of str1 in str2
How to use C# string Equals
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str1 = "Equals";
string str2 = "Equals";
if (string.Equals(str1, str2))
{
MessageBox.Show("Strings are Equal() ");
}
else
{
MessageBox.Show("Strings are not Equal() ");
}
}
}
}
When you run this C# program you will get message "Strings are Equal() "
How to use C# string Insert
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str = "This is CSharp Test";
string insStr = "Insert ";
string strRes = str.Insert(15, insStr);
MessageBox.Show(strRes);
}
}
}
When you execute this C# program you will get the message box
showing "This is CSharp Insert Test"
How to use C# string Contains
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str = null;
str = "CSharp TOP 10 BOOKS";
if (str.Contains("TOP") == true)
{
MessageBox.Show("The string Contains() 'TOP' ");
}
else
{
MessageBox.Show("The String does not Contains() 'TOP'");
}
}
}
}
When you run the C# program you will get the MessageBox with message
"The string Contains() 'TOP' "
How to use C# string Compare
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str1 = null;
string str2 = null;
str1 = "csharp";
str2 = "CSharp";
int result = 0;
result = string.Compare(str1, str2);
MessageBox.Show(result.ToString());
result = string.Compare(str1, str2, true);
MessageBox.Show(result.ToString());
}
}
}
When you run this C# program first u will get -1 in message box and then 0
How to use C# string IndexOf
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str = null;
str = "CSharp TOP 10 BOOKS";
MessageBox.Show(str.IndexOf("BOOKS").ToString());
}
}
}
When you execute this C# program you will get the number 14 in the message box.
That means the substring "BOOKS" occurred and start in the position 14.
Monday, February 27, 2017
How to validate a string in C#
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bool isNumeric;
int i;
string str = "100";
isNumeric = int.TryParse(str, out i);
MessageBox.Show("The value of i is " + i);
}
}
}
How to use C# string Format
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
double dNum = 0;
dNum = 32.123456789;
MessageBox.Show("Formated String " + string.Format("{0:n4}", dNum));
}
}
}
When you run this C# source code you will get "Formated String 32.1234"
How to use C# string Length
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str = null;
str = "This is a Test";
MessageBox.Show(str.Length.ToString());
}
}
}
When you execute this C# source code you will get 14 in the messagebox.
How to use C# string Clone
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
{
string str = "Clone() Test";
string clonedString = null;
clonedString = (String)str.Clone();
MessageBox.Show (clonedString);
}
}
}
}
When you run this C# program you will get the same content of the
first string "Clone() Test"
How to use C# string Concat
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str1 = null;
string str2 = null;
str1 = "Concat() ";
str2 = "Test";
MessageBox.Show(string.Concat(str1, str2));
}
}
}
When you run this C# source code you will get "Concat() Test " in message box.
How to use C# string Split
Subscribe to:
Posts (Atom)