Examples used by the string class (3)

Source: Internet
Author: User
Tags arrays join joins readline trim
if ("==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 ("==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 ' Insert Ed 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 Usi Ng ' str ' starting from the ' I ' ' i ' th ' ' ' I ' ' ' ' ' ' I ' 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]= '", 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]= '", 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" is the last occurence of the 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 th E string has been reached ");

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

}

private void Mtdlastindexany () {
Console.WriteLine ("String.lastindexofany ()-> This returns", "the" last occurence an any charcter of the Char Acter 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 th E string has been reached ");

Console.WriteLine ("It returns the index if the" 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 Lengt H of the string along with spaces ");

Console.WriteLine ("String.pad" +strval+ "(int i,char c)-> fills the character ' C ' to" "+strval+" of the String, ' I ' Pecifies 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" Character to be padded (enter no 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 thr Oughout the given string ");

Console.WriteLine ("1"). String.Replace (char Cold,char cnew)-> replaces all occurances ' cOld ' and ' cnew ' ");

Console.WriteLine ("2"). String.Replace (String Sold,strin snew)-> replaces all occurances of ' sold ' and ' 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 would 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 has 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" 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" 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 spaces characters from the begininning and end of the String and also Specified characters. ");

Console.WriteLine ("1"). String.Trim ()-> removes white spaces characters from beginning and end of the string. ");

Console.WriteLine ("2"). String.Trim (char[] c)-> removes all occurances to the set of characters in the ' 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 following 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 E nd 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 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));
}
} (Posted)

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.