Foundation Strengthening the next day string basics exercise

Source: Internet
Author: User

String attributes
Exercise 1. Display the contact name and phone number in the CSV file. Simple simulation CSV file, CSV file is used, split the text of the data, output: Name: Zhang San Tel: 15001111113


String[] Lines =system.io.file.readalllines ("1.csv", encoding.default);//Read all the rows in the file into the array.
for (int i = 0; i < lines. Length; i++)
{
string[] linesnew = lines[i]. Replace (",", ""). Split (New char[]{' '},stringsplitoptions.removeemptyentries);


Console.WriteLine ("Name: {0}", Linesnew[0] + linesnew[1]);
Console.WriteLine ("Tel: {0}", linesnew[2]);
}
Console.readkey ();
Exercise 2, ask for the average wage
list<int> list = new list<int> ();
string[] txt = System.IO.File.ReadAllLines ("3.txt", Encoding.default);
for (int i = 0; i < txt. Length; i++)
{
string[] listnew = Txt[i]. Split (new[] {', '},stringsplitoptions.removeemptyentries);
Add everyone's wages to the collection
List. ADD (Convert.ToInt32 (listnew[1));
}
Console.WriteLine ("Average salary is {0}", list.) Average ());
Console.readkey ();


String Practice
#region 1, receives the user input string, the character utilises and the input in the reverse order output. "abc" → "CBA".
Prompt user for Input string
Console.WriteLine ("Please enter a string");
String to accept user input
String msg = Console.ReadLine ();
Write a method to do this
Assigns the returned value to the MSG
msg = changestring (msg);
Console.WriteLine (msg);
Console.readkey ();
#endregion
Because the main function is static, we define it as a static function.
public static string Changestring (String msg)
{
Convert a string to a string array
char[] Cha = Msg. ToCharArray ();
Define a third-party variable
char temp;
For loop operation
for (int i = 0; i < cha. LENGTH/2; i++)
{
temp=cha[i];//the first item of a string
Cha[i]=cha[cha. length-i-1];//the last item of a string
Cha[cha. Length-i-1] = temp;//First and last Exchange
}
return new string (CHA);
}

String processing Exercises


#region Exercise 8: Ask for the employee's salary in the document, the maximum wage, the minimum wage, the average wage
Define a generic collection to hold wages
list<int> list = new list<int> ();
Read files, remove garbled characters
string[] str = System.IO.File.ReadAllLines ("Salary.txt", Encoding.default);
For Loop read
for (int i = 0; i < str. Length; i++)
{
Remove the equals sign to return a new array
string[] strnew = Str[i]. Split (new char[]{' = '},stringsplitoptions.removeemptyentries);
Add the wages in the array to the collection by using the wages that are obtained from the index
List. ADD (Convert.ToInt32 (strnew[1));
}
Console.WriteLine ("Maximum wage: {0}, minimum wage: {1}, average salary {2}", List. Max (), list. Min (), list. Average ());
Console.readkey ();
#endregion

#region Practice 5.0:123-456---7--------123----2 To remove the repeating symbol "-" in a similar string, getting 123-456-7-89-123-2. Split (), Stringsplitoptions.removeemptyentries Join ()
Get the current string
String str = "123-456---7--------123----2";
Remove the "-" in the string and return a new array
string[] Newstr = str. Split (New char[]{'-'},stringsplitoptions.removeemptyentries);
The resulting new string is concatenated with "-" and then re-assigned to the string.
str = string. Join ("-", newstr);
Console.WriteLine (str);
Console.readkey ();
#endregion

Practice

#region 1. Determine if the user is entering a mailbox
Console.WriteLine ("Please enter email");
string email = console.readline ();
int index = email. IndexOf ("@");
if (Index! =-1)
{
Console.WriteLine ("is Mailbox");
}
Else
{
Console.WriteLine ("Not Mailbox");
}
Console.readkey ();
#endregion

Foundation Strengthening the next day string basics exercise

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.