This program explores the String class and its various methods. With this you will be able to initialize strings using different constructors and use them. To see how the methods of the 'String' class work, see the following code.
The program provides a menu which helps in exploring the methods. Each method is presented with its syntax and and the end result if that method were to be applied to the string.
Source Code
/*
This program explores the 'String' class, with this you will be able to create the string using the constructors and explore all the methods in this class.
*/
using System;
class myString
{
public String str;
public myString (char[] strValue)
{
str = new String(strValue);
Console.WriteLine("The string '" + str + "' has been initialised by passing an array of characters");
}
public myString (char strValue,int intCount)
{
str = new String(strValue,intCount);
Console.WriteLine("The string '" + str + "' has been initialised by a character '" + strValue + "' which is repeated '" + intCount + "' times");
}
public myString (char[] strValue,int intStart,int intEnd)
{
str = new String(strValue,intStart,intEnd);
Console.WriteLine("The string "+ str + " has been initialised by array of characters starting from '"+intStart+"' and ending at '"+intEnd+"'");
}
}
public class clsString
{
char[] chArray; //={'c','h','a','r','a','c','t','e','r',' ','a','r','r','a','y'};
int intChoice;
myString objString;
public static void Main()
{
clsString obj = new clsString();
obj.init();
}
private void init()
{
Console.WriteLine("1. Create the string through character Array");
Console.WriteLine("2. Create the string through a single character which is repeated a number of times");
Console.WriteLine("3. Create the string through character array specifying the starting and ending places");
Console.WriteLine("4. To exit");
Console.Write("Enter Your Choice : " );
string strTemp=Console.ReadLine();
intChoice=int.Parse(strTemp);
try
{
switch (intChoice)
{
case 1:
mtdCharacterArray();
break;
case 2:
mtdRepetions();
break;
case 3:
mtdChArrayStEnd();
break;
default:
Console.WriteLine("default");
Environment.Exit(0);
break;
}
mtdMethods();
init();
}
catch (Exception e)
{
Console.WriteLine("error occured at :" + e.StackTrace);
}
}
private void mtdCharacterArray()
{
chArray=mtdGetChArray();
objString = new myString(chArray);
}
private char[] mtdGetChArray()
{
char[] chTempArray = new char[Char.MaxValue];
string strTemp;
Console.Write("Enter the character(s) : ");
strTemp=Console.ReadLine();
chTempArray=strTemp.ToCharArray();
return chTempArray;
}
private void mtdRepetions()
{
Console.Write("Give the number of Repetions : ");
string strTmp=Console.ReadLine();
int intTemp=int.Parse(strTmp);
chArray=mtdGetChArray();
objString = new myString(chArray[0],intTemp);
}
private void mtdChArrayStEnd()
{
string strTmp;
Console.Write("Give the starting position :");
strTmp=Console.ReadLine();
int intStart=int.Parse(strTmp);
Console.Write("Give the ending position : ");
strTmp=Console.ReadLine();
int intEnd=int.Parse(strTmp);
chArray=mtdGetChArray();
objString = new myString(chArray,intStart,intEnd);
}
private void mtdMethods()
{
Console.WriteLine("");
Console.WriteLine("1. Concatenate strings ");
Console.WriteLine("2. Copy of a String into an existing one.");
Console.WriteLine("3. Create a new string out of the charecters in the original one");
Console.WriteLine("4. Check if the string ends with a particular set of characters");
Console.WriteLine("5. Formating the string");
Console.WriteLine("6. Hash code of the string");
Console.WriteLine("7. Index of first occurence of any string within another string");
Console.WriteLine("8. Index of first occurence of any character in the given string");
Console.WriteLine("9. Insert a string at a specified place");
Console.WriteLine("10. Join the strings with a seperator");
Console.WriteLine("11. Index of last occurence of any string within another string");
Console.WriteLine("12. Index of last occurence of any character in the given string");
Console.WriteLine("13. Length of the string");
Console.WriteLine("14. Pad required number of spaces or any other character to the left of the string");
Console.WriteLine("15. Pad required number of spaces or any other character to the right of the string");
Console.WriteLine("16. Remove some characters from a specified location");
Console.WriteLine("17. Replaces all occurences of a specified string in another string");
Console.WriteLine("18. Split the string depending on a delimiter");
Console.WriteLine("19. Check if the string starts with a particular 'string'");
Console.WriteLine("20. Retrieve a substring");
Console.WriteLine("21. Get the string in lower case");
Console.WriteLine("22. Get the string in UPPER case");
Console.WriteLine("23. Trim the String");
Console.WriteLine("24. Trim the end of the string");
Console.WriteLine("25. Trim the start of the string");
Console.WriteLine("26. Exit the program");
Console.WriteLine("");
Console.Write("What do u want to do now : " );
string strMtd=Console.ReadLine();
Console.WriteLine("");
try
{
int intSwitch=int.Parse(strMtd);
switch (intSwitch)
{
case 1:
mtdConcatenate();
break;
case 2:
mtdCopy();
break;
case 3:
mtdCopyTo();
break;
case 4:
mtdEndsWith();
break;
case 5:
mtdFormat();
break;
case 6:
mtdHash();
break;
case 7:
mtdIndexOf();
break;
case 8:
mtdIndexOfAny();
break;
case 9:
mtdInsert();
break;
case 10:
mtdJoin();
break;
case 11:
mtdLastIndex();
break;
case 12:
mtdLastIndexAny();
break;
case 13:
mtdLength();
break;
case 14:
mtdPadLeft();
break;
case 15:
mtdPadRight();
break;
case 16:
mtdRemove();
break;
case 17:
mtdReplace();
break;
case 18:
mtdSplit();
break;
case 19:
mtdStartsWith();
break;
case 20:
mtdSubStr();
break;
case 21:
mtdLower();
break;
case 22:
mtdUpper();
break;
case 23:
mtdTrim();
break;
case 24:
mtdTrimEnd();
break;
case 25:
mtdTrimStart();
break;
case 26:
Environment.Exit(0);
break;
default:
Console.WriteLine("Wake up !!!! . Select a correct choice");
break;
}
mtdMethods();
}
catch (Exception e)
{
Console.WriteLine("Error : " + e.Message);
Console.WriteLine("Source: " + e.StackTrace);
Environment.Exit(0);
}
}
private void mtdConcatenate()
{
string[] strTempArr=new String[10];
Console.WriteLine("String.Concat() - > this concatenates strings together.There are 4 implementations");
Console.WriteLine("1. String.Concat(String[]) -> concatenates the string array");
Console.WriteLine("2. String.Concat(String,String) -> concatenates the two strings");
Console.WriteLine("3. String.Concat(String,String,String) -> concatenates the three strings ");
Console.WriteLine("4. String.Concat(String,String,String,String) -> concatenates the four strings ");
Console.Write("Enter the string array length : ");
string strArr=Console.ReadLine();
int intArr=int.Parse(strArr);
for (int i=0;i<intArr;i++)
{
Console.Write("Enter string " + i + " : ");
strTempArr[i]=Console.ReadLine();
}
Console.WriteLine("the Concatenated string : " + String.Concat(strTempArr));
Console.WriteLine("the concatenation of the first two string : " + String.Concat(strTempArr[0],strTempArr[1]));
Console.WriteLine("the concatenation of the first three string : " + String.Concat(strTempArr[0],strTempArr[1],strTempArr[2]));
Console.WriteLine("the concatenation of the first four string : " + String.Concat(strTempArr[0],strTempArr[1],strTempArr
2],strTempArr[3]));
}
private void mtdCopy()
{
Console.WriteLine("String.Copy(String str) - > returns a new string with the same value as 'str'");
Console.WriteLine("original string : " + objString.str);
Console.Write("enter the string to replace the above one : ");
string strCopy=Console.ReadLine();
objString.str=String.Copy(strCopy);
Console.WriteLine("the string after copying : " + objString.str);
}
private void mtdCopyTo()
{
Console.WriteLine("String.CopyTo(int srcIndex,char[] dest,int destIndex,int intCount) - > copies a part of string to another string");
Console.WriteLine("srcIndex -> the start index in the original string from where u want the copy");
Console.WriteLine("dest -> the destination chracter array");
Console.WriteLine("destIndex -> the start index in the destination array to which the characters should be copied");
Console.WriteLine("dest -> the length of characters in the original string to be copied");
Console.WriteLine("Destination string is : " + objString.str);
Console.Write("Enter the source string : ");
string strTmp=Console.ReadLine();
Console.Write("Enter the starting index for source string : ");
string strSrcSt=Console.ReadLine();
int intSrcSt=int.Parse(strSrcSt);
Console.Write("Enter the starting index in the destination string : ");
string strDstSt=Console.ReadLine();
int intDstSt=int.Parse(strDstSt);
Console.Write("Enter the number of characters to be copied from the source string : ");
string strSrcLn=Console.ReadLine();
int intSrcLn=int.Parse(strSrcLn);
chArray=objString.str.ToCharArray();
strTmp.CopyTo(intSrcSt,chArray,intDstSt,intSrcLn);
objString.str=new String(chArray);
Console.WriteLine("The changed string is : " + objString.str);
}
private void mtdEndsWith()
{
Console.WriteLine("String.EndsWith(String str) - > this function returns a boolen value, checking whether the parent string ends with 'str'");
Console.WriteLine("The string to be checked :" + objString.str);
Console.Write("Enter the 'ends with' string :");
String strTmp = Console.ReadLine();
if (objString.str.EndsWith(strTmp))
Console.WriteLine("'"+ objString.str + "' ends with '" + strTmp + "'.");
else
Console.WriteLine("'" + objString.str + "' does not end with '" + strTmp + "'.");
}
private void mtdFormat()
{
Console.WriteLine("String.Format() - > this static function helps in formating the strings");
Console.WriteLine("Format(String str,Object obj) -> format the string with one object");
Console.WriteLine("Format(String str,Object obj1,Object obj2) -> format the string with two objects");
Console.WriteLine("Formating the string with three objects is another implementation.");
Console.WriteLine("The string should follow the formating specification - > {N,[M]:[formatstring]}");
Console.WriteLine("N -> indicates the argument to be replaced. starts from zero(0)");
Console.WriteLine("M -> indicates the length of formatting area,padded with spaces if the value filled in is smaller.");
Console.WriteLine("if the value is -ve then value is left justified, if value is +ve then value is right justified.");
Console.WriteLine("formatstring -> these are values of formating codes (eg : 'C' is used for currency)");
String strTmp="it is {0} working in {1,10} bcos i get {2,5:C} per month.it is not {0} working {1,-10} bcos i get only {2,-5:C}";
Console.WriteLine("The source string is : " + strTmp);
Console.Write("Enter the first replacement string : ");
String strTmp1=Console.ReadLine();
Console.Write("Enter the second replacement string : ");
String strTmp2=Console.ReadLine();
Console.Write("Enter a numeral : ");
String strTmp3=Console.ReadLine();
int intTmp=int.Parse(strTmp3);
Console.WriteLine("the modified string :" + String.Format(strTmp,strTmp1,strTmp2,intTmp));
}
private void mtdHash()
{
Console.WriteLine("String.GetHashCode() - > this fuctions returns the hash code of the string");
Console.WriteLine("Hash of '"+ objString.str + "' is : " + objString.str.GetHashCode());
}
private void mtdIndexOf()
{
Console.WriteLine("String.IndexOf() - > this returns the index of the first occurence of a charcter or string in the given string.");
Console.WriteLine("The search of the string stops when the required value is founds or proceedes until the end of the string has been reached");
Console.WriteLine("It returns the index if the value is found or '-1' if not found.");
mtdIndexImpl("Index","first");
}
private void mtdIndexImpl(String strValue,String strFL)
{
string strChar;
char c;
int intStart;
int intCount;
Console.WriteLine("1. String."+strValue+"Of(char c) -> returns the "+strFL+" occurence 'c' in the string");
Console.WriteLine("2. String."+strValue+"Of(string str) -> returns the "+strFL+" occurence of 'str' in the string");
Console.WriteLine("3. String."+strValue+"Of(char c,int i) -> returns the "+strFL+" occurence of 'c' in the string, the search starts from 'i'");
Console.WriteLine("4. String."+strValue+"Of(string str,int i) -> returns the "+strFL+" occurence of 'str' in the string, the search starts from 'i'");
Console.WriteLine("5. String."+strValue+"Of(char c,int i,int j) -> returns the "+strFL+" occurence of 'c' in the string, the search starting from 'i' and examining 'j' character positions.");
Console.WriteLine("6. String."+strValue+"Of(string str,int i,int j) -> returns the "+strFL+" occurence of 'str' in the string, the search starting from 'i' and examining 'j' character positions.");
Console.WriteLine("7. Finished with "+strValue+"Of.");
Console.Write("Give the choice :");
String strTmp=Console.ReadLine();
int intChoice=int.Parse(strTmp);
bool blnStay=true;
Console.WriteLine("The Source string is :"+objString.str);
switch (intChoice)
{
case 1:
Console.Write("Enter the character :");
strChar=Console.ReadLine();
c=(strChar.ToCharArray())[0];
if ("first"==strFL)
Console.WriteLine("The index of '"+c+"' in '"+objString.str+"' is : "+objString.str.IndexOf(c));
else
Console.WriteLine("The index of '"+c+"' in '"+objString.str+"' is : "+objString.str.LastIndexOf(c));
break;
case 2:
Console.Write("Enter the string :");
strChar=Console.ReadLine();
if ("first"==strFL)
Console.WriteLine("The index of '"+strChar+"' in '"+objString.str+"' is : "+objString.str.IndexOf(strChar));
else
Console.WriteLine("The index of '"+strChar+"' in '"+objString.str+"' is : "+objString.str.LastIndexOf(strChar));
break;
case 3:
Console.Write("Enter the character :");
strChar=Console.ReadLine();
c=(strChar.ToCharArray())[0];
Console.Write("Enter the starting Index :");
intStart=int.Parse(Console.ReadLine());
if ("first"==strFL)
Console.WriteLine("The index of '"+c+"' in '"+objString.str+"' is : "+objString.str.IndexOf(c,intStart));
else
Console.WriteLine("The index of '"+c+"' in '"+objString.str+"' is : "+objString.str.LastIndexOf(c,intStart));
break;
case 4:
Console.Write("Enter the string :");
strChar=Console.ReadLine();
Console.Write("Enter the starting Index :");
intStart=int.Parse(Console.ReadLine());
if ("first"==strFL)
Console.WriteLine("The index of '"+strChar+"' in '"+objString.str+"' is : "+objString.str.IndexOf(strChar,intStart));
else
Console.WriteLine("The index of '"+strChar+"' in '"+objString.str+"' is : "+objString.str.LastIndexOf(strChar,intStart));
break;
case 5:
Console.Write("Enter the character :");
strChar=Console.ReadLine();
c=(strChar.ToCharArray())[0];
Console.Write("Enter the starting Index :");
intStart=int.Parse(Console.ReadLine());
Console.Write("Enter the number of characters to search : ");
intCount=int.Parse(Console.ReadLine());
if ("first"==strFL)
Console.WriteLine("The index of '"+c+"' in '"+objString.str+"' is : "+objString.str.IndexOf(c,intStart,intCount));
else
Console.WriteLine("The index of '"+c+"' in '"+objString.str+"' is : "+objString.str.LastIndexOf(c,intStart,intCount));
break;
case 6:
Console.Write("Enter the character :");
strChar=Console.ReadLine();
Console.Write("Enter the starting Index :");
intStart=int.Parse(Console.ReadLine());
Console.Write("Enter the number of characters to search : ");
intCount=int.Parse(Console.ReadLine());
if ("first"==strFL)
Console.WriteLine("The index of '"+strChar+"' in '"+objString.str+"' is : "+objString.str.IndexOf(strChar,intStart,intCount));
else
Console.WriteLine("The index of '"+strChar+"' in '"+objString.str+"' is : "+objString.str.LastIndexOf(strChar,intStart,intCount));
break;
case 7:
blnStay=false;
break;
}
if (blnStay)
mtdIndexImpl(strValue,strFL);
}
private void mtdIndexOfAny()
{
Console.WriteLine("String.IndexOfAny() - > this returns the index of the first occurence of any charcter of the character array in the given string.");
Console.WriteLine("The search of the string stops when the required value is founds or proceedes until the end of the string has been reached");
Console.WriteLine("It returns the index if the value is found or '-1' if not found.");
mtdIndexAnyImpl("Index","first");
}
private void mtdIndexAnyImpl(String strValue,String strFL)
{
string strChar;
char[] c=new char[char.MaxValue];
int intStart;
int intCount;
Console.WriteLine("1. String."+strValue+"OfAny(char[] c) -> returns the "+strFL+" occurence of any character of the array in the string");
Console.WriteLine("2. String."+strValue+"OfAny(char[] c,int i) -> returns the "+strFL+" occurence of any character of the array in the string, the search starts from 'i'");
Console.WriteLine("3. String."+strValue+"Of(char[] c,int i,int j) -> returns the "+strFL+" occurence of any character of the array in the string, the search starting from 'i' and examining 'j' character positions.");
Console.WriteLine("4. Finished with "+strValue+"OfAny.");
Console.Write("Give the choice :");
String strTmp=Console.ReadLine();
int intChoice=int.Parse(strTmp);
bool blnStay=true;
Console.WriteLine("The source string is : "+objString.str );
switch (intChoice)
{
case 1:
Console.Write("Enter the string for the character array :");
strChar=Console.ReadLine();
c=strChar.ToCharArray();
if ("first"==strFL)
Console.WriteLine("The index value returned is : "+ objString.str.IndexOfAny(c));
else
Console.WriteLine("The index value returned is : "+ objString.str.LastIndexOfAny(c));
break;
case 2:
Console.Write("Enter the string for the character array :");
strChar=Console.ReadLine();
c=strChar.ToCharArray();
Console.Write("Enter the starting Index for search :");
intStart=int.Parse(Console.ReadLine());
if ("first"==strFL)
Console.WriteLine("The index value returned is : "+ objString.str.IndexOfAny(c,intStart));
else
Console.WriteLine("The index value returned is : "+ objString.str.LastIndexOfAny(c,intStart));
break;
case 3:
Console.Write("Enter the string for the character array :");
strChar=Console.ReadLine();
c=strChar.ToCharArray();
Console.Write("Enter the starting Index for search :");
intStart=int.Parse(Console.ReadLine());
Console.Write("Enter the number of characters to searc :");
intCount=int.Parse(Console.ReadLine());
if ("first"==strFL)
Console.WriteLine("The index value returned is : "+ objString.str.IndexOfAny(c,intStart,intCount));
else
Console.WriteLine("The index value returned is : "+ objString.str.LastIndexOfAny(c,intStart,intCount));
break;
case 4: blnStay=false;
break;
}
if (blnStay)
mtdIndexAnyImpl(strValue,strFL);
}
private void mtdInsert()
{
Console.WriteLine("String.Insert(int index,string str) - > this functions returns the original string with 'str' inserted at 'index'");
Console.WriteLine("the original string : " + objString.str);
Console.Write("Enter the string to be inserted : ");
string strTmp=Console.ReadLine();
Console.Write("Enter the position where it has to be inserted :");
int intTmp=int.Parse(Console.ReadLine());
objString.str=objString.str.Insert(intTmp,strTmp);
Console.WriteLine("the modified string : " + objString.str);
}
private void mtdJoin()
{
string[] s={"welcome","to","the","world","of","c#"};
Console.WriteLine("1.String.Join(string str,string[] strarr) - > this functions joins the string arrays using 'str'");
Console.WriteLine("2.String.Join(string str,string[] strarr,int i,int j) - > this functions joins the string arrays using 'str' starting from the 'i' th array element and 'j' number of elements after it. ");
Console.Write("Enter your choice :");
string strChoice=Console.ReadLine();
if ("1"==strChoice)
{
Console.WriteLine("The string array is :str[0]='welcome',str[1]='to',str[2]='the',str[3]='world',str[4]='of',str[5]='c#'");
Console.Write("Enter the string with which to join : ");
string strTmp=Console.ReadLine();
Console.WriteLine("The joint string is : " + String.Join(strTmp,s));
}
else
{
Console.WriteLine("The string array is :str[0]='welcome',str[1]='to',str[2]='the',str[3]='world',str[4]='of',str[5]='c#'");
Console.Write("Enter the string with which to join : ");
string strTmp=Console.ReadLine();
Console.Write("Enter the starting index of the array : ");
int intStart=int.Parse(Console.ReadLine());
Console.Write("Enter the number of elements to join :" );
int intCount=int.Parse(Console.ReadLine());
Console.WriteLine("The joint string is : " + String.Join(strTmp,s,intStart,intCount));
}
}
private void mtdLastIndex()
{
Console.WriteLine("String.LastIndexOf() - > this returns the index of the last occurence of a charcter or string in the given string.");
Console.WriteLine("The search of the string stops when the required value is founds or proceedes until the beginning of the string has been reached");
Console.WriteLine("It returns the index if the value is found or '-1' if not found.");
mtdIndexImpl("LastIndex","last");
}
private void mtdLastIndexAny()
{
Console.WriteLine("String.LastIndexOfAny() - > this returns the index of the last occurence of any charcter of the character array in the given string.");
Console.WriteLine("The search of the string stops when the required value is founds or proceedes until the beginning of the string has been reached");
Console.WriteLine("It returns the index if the value is found or '-1' if not found.");
mtdIndexAnyImpl("LastIndex","last");
}
private void mtdLength()
{
Console.WriteLine("String.Length - > this property returns the length of the string.");
Console.WriteLine("The length of '"+objString.str+"' is : "+objString.str.Length);
}
private void mtdPadLeft()
{
mtdPad("Left");
}
private void mtdPad(String strVal)
{
Console.WriteLine("String.Pad"+strVal+"() - > this method pads spaces or some other character to the "+strVal+" of the string");
Console.WriteLine("String.Pad"+strVal+"(int i) -> fills spaces to the "+strVal+" of the string, 'i' specifies the length of the string along with spaces");
Console.WriteLine("String.Pad"+strVal+"(int i,char c) -> fills the character 'c' to the "+strVal+" of the string, 'i' specifies the length of the string along with spaces");
Console.WriteLine("The original string :"+objString.str );
Console.Write("Enter the length of the desired string : ");
int intStart=int.Parse(Console.ReadLine());
Console.Write("Enter the Character to be padded(enter nothing for spaces) :");
string strTmp=Console.ReadLine();
if(!strTmp.Equals(""))
{
char c=(strTmp.ToCharArray())[0];
if ("Left"==strVal)
Console.WriteLine("The padded string : " + objString.str.PadLeft(intStart,c));
else
Console.WriteLine("The padded string : " + objString.str.PadRight(intStart,c));
}
else
if ("Left"==strVal)
Console.WriteLine("The padded string : " + objString.str.PadLeft(intStart));
else
Console.WriteLine("The padded string : " + objString.str.PadRight(intStart));
}
private void mtdPadRight()
{
mtdPad("Right");
}
private void mtdRemove()
{
Console.WriteLine("String.Remove(int i,int j) - > removes a part of the string.'i' represents the start position and 'j' represents the length of string to be removed.");
Console.WriteLine("The original string : "+objString.str);
Console.Write("Enter the starting position : ");
int intStart=int.Parse(Console.ReadLine());
Console.Write("Enter the length of string to be removed :");
int intLength=int.Parse(Console.ReadLine());
Console.WriteLine("The string after removal :"+objString.str.Remove(intStart,intLength));
}
private void mtdReplace()
{
Console.WriteLine("String.Replace() - > replaces a character with another character or a string with another string throughout the given string");
Console.WriteLine("1. String.Replace(char cOld,char cNew) -> replaces all occurances 'cOld' with 'cNew'");
Console.WriteLine("2. String.Replace(string sOld,strin sNew) -> replaces all occurances of 'sOld' with 'sNew'");
Console.Write("Enter your choice :");
int intChoice=int.Parse(Console.ReadLine());
Console.WriteLine("The original string is :"+objString.str);
if (1==intChoice)
{
Console.Write("Enter the character to be replaced :");
char cold=(Console.ReadLine().ToCharArray())[0];
Console.Write("Enter the new character :");
char cnew=(Console.ReadLine().ToCharArray())[0];
Console.WriteLine("The string after replacing : "+objString.str.Replace(cold,cnew));
}
else
{
Console.Write("Enter the string to be replaced :");
string sold=Console.ReadLine();
Console.Write("Enter the new string :");
string snew=Console.ReadLine();
Console.WriteLine("The string after replacing : "+objString.str.Replace(sold,snew));
}
}
private void mtdSplit()
{
Console.WriteLine("This will be done later.");
}
private void mtdStartsWith()
{
Console.WriteLine("String.StartsWith(string str) - > returns a boolean value indicating whether the string starts with 'str'");
Console.WriteLine("The original string : "+ objString.str);
Console.Write("Enter the string to search for :");
string strTmp=Console.ReadLine();
if (objString.str.StartsWith(strTmp))
Console.WriteLine("The string '"+objString.str+"' starts with '"+strTmp+"'.");
else
Console.WriteLine("The string '"+objString.str+"' does not starts with '"+strTmp+"'.");
}
private void mtdSubStr()
{
Console.WriteLine("String.Substring() - > retrieves a part of the string from the original string");
Console.WriteLine("1. String.Substring(int i) -> retrieves the string starting from 'i'(zero based)");
Console.WriteLine("2. String.Substring(int i,int j) -> retrieves the string starting from 'i' and having a length 'j'.");
Console.Write("Enter your choice :");
int intChoice=int.Parse(Console.ReadLine());
int intStart,intLength;
Console.WriteLine("The original string :"+objString.str);
if (1==intChoice)
{
Console.Write("Enter the position from where the substring should start :");
intStart=int.Parse(Console.ReadLine());
Console.WriteLine("The retrieved substring is :"+objString.str.Substring(intStart));
}
else
{
Console.Write("Enter the position from where the substring should start :");
intStart=int.Parse(Console.ReadLine());
Console.Write("Enter the length of the substring:");
intLength=int.Parse(Console.ReadLine());
Console.WriteLine("The retrieved substring is :"+objString.str.Substring(intStart,intLength));
}
}
private void mtdLower()
{
Console.WriteLine("String.ToLower() - > returns the string with all its characters in lower case");
Console.WriteLine("The original string : " + objString.str);
Console.WriteLine("The string in lower case : " +objString.str.ToLower());
}
private void mtdUpper()
{
Console.WriteLine("String.ToUpper() - > returns the string with all its characters in upper case");
Console.WriteLine("The original string : " + objString.str);
Console.WriteLine("The string in upper case : " +objString.str.ToUpper());
}
private void mtdTrim()
{
Console.WriteLine("String.Trim() - > removes white space characters from the begininning and end of the string and also specified characters.");
Console.WriteLine("1. String.Trim() -> removes white space characters from beginning and end of the string.");
Console.WriteLine("2. String.Trim(char[] c) -> removes all occurances of set of characters in the array from the beginning and end of string.");
Console.Write("Enter your choice :");
int intChoice=int.Parse(Console.ReadLine());
Console.WriteLine("The original string : " +objString.str);
if (1==intChoice)
{
Console.WriteLine("The trimmed string is : "+objString.str.Trim());
}
else
{
Console.Write("Enter the character array : ");
char[] c=Console.ReadLine().ToCharArray();
Console.WriteLine("The string after removing characters from the array : " + objString.str.Trim(c));
}
}
private void mtdTrimEnd()
{
Console.WriteLine("String.TrimEnd(char[] c) - > removes all occurances of the set of characters in the array from the end of the string.");
Console.WriteLine("The original string is : " + objString.str);
Console.Write("Enter the character array : ");
char[] c=Console.ReadLine().ToCharArray();
Console.WriteLine("The modified string is : "+objString.str.TrimEnd(c));
}
private void mtdTrimStart()
{
Console.WriteLine("String.TrimStart(char[] c) - > removes all occurances of the set of characters in the array from the start of the string.");
Console.WriteLine("The original string is : " + objString.str);
Console.Write("Enter the character array : ");
char[] c=Console.ReadLine().ToCharArray();
Console.WriteLine("The modified string is : "+objString.str.TrimStart(c));
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}