String类使用的例子(1)
2024-07-21 02:20:14
供稿:网友
 
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 "); 
(转贴)