| 
| 技术资料  > .Net专区 > Asp.Net开发 : String类使用的例子(3) |  
String类使用的例子(3) March 25,2004 |  
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));  
}  
}  
         |  
 
 |