String类使用的例子(2)

80酷酷网    80kuku.com

  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());
(转贴)

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: