String Manipulations

 Def: A string is a collection of characters.
 In C#, each string will be recognized as an object for “System.String” class.

The “System.String” class

 “String” class provides necessary properties and methods for performing all the common and advanced manipulations on strings.
 Here are the properties of “String ” class.


Property Description Syntax
Length Gets the total no. of characters in the string object. stringobj.Length

Methods of “String” Class

Property Description Syntax
ToLower() Converts the string into lower case and returns it. stringobj.ToLower()
ToUpper() Converts the string into upper case and returns it. stringobj.ToUpper()
Trim() Removes the blank spaces at left and right side of the string and returns it. stringobj.Trim()
TrimStart() Removes the blank spaces at left of the string and returns it. stringobj.TrimStart()
TrimEnd() Removes the blank spaces at right side of the string and returns it. stringobj.TrimEnd()
Equals() Checks the string with the given string for equality. If both are equal, returns “true”, otherwise returns “false”. stringobj.Equals(str)
StartsWith() Checks whether the string contains the given string at the left most side. If it contains, returns “true”, otherwise returns “false”. stringobj.StartsWith(str)
EndsWith() Checks whether the string contains the given string at the right most side. If it contains, returns “true”, otherwise returns “false”. stringobj.EndsWith(str)
Insert() Inserts the given string value into the string object at the specific index and returns it. stringobj.Insert(index, stringvalue)
Remove() Removes all the characters from the string, starting from given index and returns it. stringobj.Remove(index)
Remove() Removes specific no. of characters (based on the given count) from the string, starting from given index and returns it. stringobj.Remove(index, count)
Substring() Gets the part of the string starting from given index up to end of the string and returns it. stringobj.Substring(index)
Substring() Gets the part of the string starting from given index based on the expected length and returns it. stringobj.Substring(index, length)
Replace() Replaces the each occurrence of given character(s) with another character(s) and returns it. stringobj.Replace(old char, new char)
PadLeft() Pads out the string by adding the given repeated character at the left side of the string and returns it. stringobj.PadLeft(total width, padding char);
PadRight() Pads out the string by adding the given repeated character at the right side of the string and returns it. stringobj.PadRight(total width, padding char);
ToCharArray() Converts the string into a character array, by treating each character in the string as an element of the character array, and returns it. stringobj.ToCharArray()
Split() Splits the string based on the given separator character and finally returns a string array. stringobj.Split(separator char);
IndexOf() Searches for existence of given character(s) in the string. If it is found (for the first occurrence), returns the index of the first character. If it is not found, then returns -1. stringobj.IndexOf(str/char)
IndexOf() Searches for existence of given character(s) in the string starting from specified index. If it is found (for the first occurrence), returns the index of the first character. If it is not found, then returns -1. stringobj.IndexOf(str/char, start index)
LastIndexOf() Searches for existence of given character(s) in the string starting from specified index. If it is found (for the first occurrence), returns the index of the first character. If it is not found, then returns -1. stringobj.IndexOf(str/char, start index)
LastIndexOf() Searches for existence of given character(s) in the string starting from specified index in reverse. If it is found (for the first occurrence), returns the index of the first character. If it is not found, then returns -1. stringobj.LastIndexOf(str/char, start index)

“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();
}
}
}
Strings
Strings