Friday, March 7, 2014

String Class

Click Here - Array Class , String Class , Math Class , Random Class

String Class

In C#, every string variable will be recognized as an object for System.String class.  The  String  class provides  necessary  properties  and  methods  for  performing  all  the common and advanced manipulations on strings.b  Here are the properties of String class.

Program for String Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StringDemo
{
class Program
{
static void Main(string[] args)
{
//create string object
string s = "I am a .NET Developer";
Console.WriteLine("The original string is: " + s);
Console.WriteLine("The original string's length is: " + s.Length);

//copy strings

string s1;
s1 = s;
Console.WriteLine("\nThe copied string is: " + s1);

//concatenate strings

string s2 = "I am working in Wipro.";
string s3;
s3 = s1 +". " + s2;
Console.WriteLine("\nThe concatenated string is: " + s3);

//compare strings

if (s1 == s2)
Console.WriteLine("\ns1 and s2 are equal");
else
Console.WriteLine("\ns1 and s2 are not equal");
string s4 = s1;
if (s1 == s4)
Console.WriteLine("\ns1 and s4 are equal");
else
Console.WriteLine("\ns1 and s4 are not equal");

//convert the string into lower case

string s6 = s.ToLower();
Console.WriteLine("\nLower case is: " + s6);

//convert the string into upper case

string s7 = s.ToUpper();
Console.WriteLine("\nUpper case is: " + s7);

//string trimming

string s8 = " Hello, World ";
Console.WriteLine("\nString before trimming: " + s8);
string s9 = s8.Trim();
Console.WriteLine("\nString after trimming: " + s9);
string s10 = s8.TrimStart();
Console.WriteLine("\nString after left trimming: " + s10);
string s11 = s8.TrimEnd();
Console.WriteLine("\nString after right trimming: " + s11);

//check for the start

if (s.StartsWith("I"))
Console.WriteLine("\nString starts with 'I'");
else
Console.WriteLine("\nString doesn't starts with 'I'");
if (s.StartsWith("i"))
Console.WriteLine("\nString starts with 'i'");
else
Console.WriteLine("\nString doesn't starts with 'i'");

//check for the end

if (s.EndsWith("Developer"))
Console.WriteLine("\nString ends with 'Developer'");
else
Console.WriteLine("\nString doesn't ends with 'Developer'");
if (s.EndsWith("am"))
Console.WriteLine("\nString ends with 'am'");
else
Console.WriteLine("\nString doesn't ends with 'am'");

//insert character(s) into the string

string s12 = s.Insert(7, "ASP");
Console.WriteLine("\nString after insetion: " + s12);

//remove character(s) from the string

string s13 = s.Remove(7);
Console.WriteLine("\nString after removal: " + s13);
string s14 = s.Remove(7, 5);
Console.WriteLine("\nString after 5 characters removal: " + s14);

//get the sub string

string s15 = s.Substring(7);
Console.WriteLine("\nSub String is: " + s15);
string s16 = s.Substring(7, 4);
Console.WriteLine("\nAnother Sub String is: " + s16);

//replace the character(s)

string s17 = s.Replace("a", "x");

Console.WriteLine("\nString after replace: " + s17); 

//left padding

string s18 = s.PadLeft(26, '*');
Console.WriteLine("\nString after left padding: " + s18);

//right padding

string s19 = s.PadRight(26, '*');
Console.WriteLine("\nString after right padding: " + s19);

//split the string

string s20 = "This is first line.This is second line.This is third line.";
Console.WriteLine("\nA string for splitting:\n" + s20);
string[] s21 = s20.Split('.');
Console.WriteLine("\nThe string array after splitting:");
foreach (string temp in s21)
Console.WriteLine(temp);

//search the string for character(s)

Console.WriteLine("Index of 'D' is: " + s.IndexOf('D'));
Console.WriteLine("Index of 'x' is: " + s.IndexOf('x'));
Console.WriteLine("Index of 'am' is: " + s.IndexOf("am"));
Console.WriteLine("Index of 'a' is: " + s.IndexOf('a'));
Console.WriteLine("Index of 'a' after 3rd character is: " + s.IndexOf('a', 3));
Console.WriteLine("LastIndex of 'D' is: " + s.LastIndexOf('D'));
Console.WriteLine("LastIndex of 'x' is: " + s.LastIndexOf('x'));
Console.WriteLine("LastIndex of 'am' is: " + s.LastIndexOf("am"));
Console.WriteLine("LastIndex of 'a' is: " + s.LastIndexOf('a'));
Console.WriteLine("LastIndex of 'a' after 3rd character is: " + s.LastIndexOf('a', 3));
Console.Read();
}
}
}

Download above Code for Demo on String Class : Download Links


Click Here - Array Class , String Class , Math Class , Random Class