String类使用的例子(3)

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
if ("first"==strFL)
Console.WriteLine("The index value returned is : "+ obj String.str.IndexOfAny(c,intStart));

else
Console.WriteLine("The index value returned is : "+ obj String.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 : "+ obj String.str.IndexOfAny(c,intStart,intCount));

else
Console.WriteLine("The index value returned is : "+ obj String.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 : " + obj String.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());
obj String.str=obj String.str.Insert(intTmp,strTmp);
Console.WriteLine("the modified String : " + obj String.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 '"+obj String.str+"' is : "+obj String.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 :"+obj String.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 : " + obj String.str.PadLeft(intStart,c));
else
Console.WriteLine("The padded String : " + obj String.str.PadRight(intStart,c));
}
else
if ("Left"==strVal)
Console.WriteLine("The padded String : " + obj String.str.PadLeft(intStart));
else
Console.WriteLine("The padded String : " + obj String.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 : "+obj String.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 :"+obj String.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 :"+obj String.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 : "+obj String.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 : "+obj String.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 : "+ obj String.str);
Console.Write("Enter the String to search for :");
String strTmp=Console.ReadLine();
if (obj String.str.StartsWith(strTmp))
Console.WriteLine("The String '"+obj String.str+"' starts with '"+strTmp+"'.");
else
Console.WriteLine("The String '"+obj String.str+"' does not starts with '"+strTmp+"'.");
}

private void mtdSubStr() {
Console.WriteLine(" String.Sub String() - > retrieves a part of the String from the original String");
Console.WriteLine("1. String.Sub String(int i) -> retrieves the String starting from 'i'(zero based)");
Console.WriteLine("2. String.Sub String(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 :"+obj String.str);
if (1==intChoice) {
Console.Write("Enter the position from where the sub String should start :");
intStart=int.Parse(Console.ReadLine());
Console.WriteLine("The retrieved sub String is :"+obj String.str.Sub String(intStart));
}
else {
Console.Write("Enter the position from where the sub String should start :");
intStart=int.Parse(Console.ReadLine());
Console.Write("Enter the length of the sub String:");
intLength=int.Parse(Console.ReadLine());
Console.WriteLine("The retrieved sub String is :"+obj String.str.Sub String(intStart,intLength));
}
}

private void mtdLower() {
Console.WriteLine(" String.ToLower() - > returns the String with all its characters in lower case");
Console.WriteLine("The original String : " + obj String.str);
Console.WriteLine("The String in lower case : " +obj String.str.ToLower());
}

private void mtdUpper() {
Console.WriteLine(" String.ToUpper() - > returns the String with all its characters in upper case");
Console.WriteLine("The original String : " + obj String.str);
Console.WriteLine("The String in upper case : " +obj String.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 : " +obj String.str);
if (1==intChoice) {
Console.WriteLine("The trimmed String is : "+obj String.str.Trim());
}
else {
Console.Write("Enter the character array : ");
char[] c=Console.ReadLine().ToCharArray();
Console.WriteLine("The String after removing characters from the array : " + obj String.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 : " + obj String.str);
Console.Write("Enter the character array : ");
char[] c=Console.ReadLine().ToCharArray();
Console.WriteLine("The modified String is : "+obj String.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 : " + obj String.str);
Console.Write("Enter the character array : ");
char[] c=Console.ReadLine().ToCharArray();
Console.WriteLine("The modified String is : "+obj String.str.TrimStart(c));
} <

seover="window.status='正文-- String 类使用的 例子(3)';return true">
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值