Examples used by the String class (2)

Source: Internet
Author: User
Tags array length bool copy hash readline
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 The" the "the" the "two string:" + String.Concat (strtemparr[0],strtemparr[1)));

Console.WriteLine ("The concatenation of the" the "the" the "three string:" + String.Concat (strtemparr[0],strtemparr[1), STRTEMPARR[2]));

Console.WriteLine ("The concatenation of the" the "the" the "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" Other string ");

Console.WriteLine ("Srcindex->" the start index in the original string from where u want the copy);
Console.WriteLine ("Dest-> destination Chracter array");
Console.WriteLine ("Destindex-> the start index in the destination array to which the characters should");

Console.WriteLine ("Dest-> the length of the 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 is 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 St Ring ends with ' str ' ");

Console.WriteLine ("The string to be checked:" + objstring.str);
Console.Write ("Enter" 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 are right justified. ");

Console.WriteLine ("FormatString-> these are values of formating codes (eg: ' C ' are used for currency)");

String strtmp= "It is {0} working in {1,10} bcos I get {2,5:c} per month.it ' not ' {0} working {1,-10} bcos I get only {2,- 5:C} ";

Console.WriteLine ("The Source string is:" + strtmp);
Console.Write ("Enter the" the "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" the "the" the "the" the "the" the "the" Ven string. ");

Console.WriteLine ("The search of the string stops when the required value is founds or proceedes until the end of the Stri Ng has been reached ");

Console.WriteLine ("It returns the index if the" is found or '-1 ' if not found. ");
Mtdindeximpl ("Index", "a");
}

private void Mtdindeximpl (String strvalue,string strfl) {
String Strchar;
char c;
int Intstart;
Int intcount;
Console.WriteLine ("1"). String. " +strvalue+ "of (char c)-> returns" +strfl+ "Occurence ' C ' in ' 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 FR Om ' i ' and examining ' j ' character positions. ");

Console.WriteLine ("7"). Finished with "+strvalue+");
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 ("==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 ("==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 ("==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 ("==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 ("==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 ("==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 "the" the "the" the "the" the "the" the "the" occurence "of" charcter Er 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 Stri Ng has been reached ");

Console.WriteLine ("It returns the index if the" is found or '-1 ' if not found. ");
Mtdindexanyimpl ("Index", "a");
}

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 the any character of the the array in the string, the SE Arch 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 ("==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 ());
Repost

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.