C# string exercises

C# string exercises

Foreword:
C# is becoming more and more confusing. Today, I took a moment to type some basic code questions and deepen my impression of strings. Ah ah ah ah, but the writing is really slow, I will improve my efficiency in the future. Not much to say, let's get to the point.


1. Output the string in the following format: "abc" —> "cba"

string[] str = new string[] {
    
     "a", "b", "c" };
for (int i = str.Length - 1; i >= 0; i--)
{
    
    
	Console.Write(str[i]);
}
Console.WriteLine();

Output result: cba (but it needs to be pointed out that this is just a method of opportunism)

string s = "abc";
StringBuilder s1 = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
    
    
	s1.Append(s[s.Length - i - 1]);
}
 s = s1.ToString();
 Console.WriteLine(s);

Here we need to know the difference between append() in StringBuilder and the'+' operation in string str. The former is to add on the original basis, while the latter is to constantly open up new space.


2. Output the string "hello c sharp" as "sharp c hello"

string s2 = "hello c sharp";
string[] str1 = s2.Split(' ');
for (int i = 0; i < str1.Length / 2; i++)
{
    
    
	string temp = str1[i];
	str1[i] = str1[str1.Length - i - 1];
	str1[str1.Length - i - 1] = temp;
}
//如果在这里输出str.length,会发现长度为3
s2 = string.Join(" ", str1);
Console.WriteLine(s2);

This question is to flash back the word instead of just a single letter, and this is also a difficulty of this question.


3. The user enters an email account and extracts the user name and domain name (@ does not belong to any one)

string str = "[email protected]";
int index = str.IndexOf('@');
for (int i = 0; i < index; i++)
{
    
    
	Console.Write(str[i]);
}
Console.WriteLine();
for (int i = index + 1; i < str.Length; i++)
{
    
    
	Console.Write(str[i]);
}
Console.WriteLine();

For this question, we first use indexof to find the position of'@', and then output the position 0-the character before'@', and output by traversal; then, traverse from the character after'@' to the end and output.


4. There are several books, each line has the title and author name, separated by a space. Output book name, the maximum length of the book name is 10, if the length of the book name exceeds 10, output the first 8 characters and add "..."; remove the space between the book name and the author name and add'|'.

string[] str = new string[]
{
    
    
	"明朝那些事儿 秦时明月",
	"我不喜欢这个世界我只喜欢你 乔一"
};
for (int i = 0; i < str.Length; i++)
{
    
    
	string[] strNew = str[i].Split(' ');
	Console.WriteLine((strNew[0].Length > 10 ? strNew[0].Substring(0, 8) + "..." : strNew[0]) + "|" + strNew[1]);
}

Create an array of string type, and then traverse each and every element. For each element in str, it is equivalent to a string of string type. In the for loop, we use the split method to turn a string into a character array. At this time, if "strNew.Length" is output, then the answer is 2. Because the array of strNew stores two elements, one is the title of the book and the other Is the name of the author. Then judge the length of the book title in the output sentence.


5. Let the user enter a sentence to query the position of e

string str = "abcdefgegklleemne";

for (int i = 0; i < str.Length; i++)
{
    
    
	if (str[i] == 'e')
	{
    
    
		Console.WriteLine(i);
	}
}
//但这个方法虽然简单,但是具有局限性,仅仅适用于单个字符的查找
//下面是通用方法
int index = 0;//用来记录'e'的位置
index = str.IndexOf('e');
Console.WriteLine("第1次出现e的位置:{0}", index);
int count = 1;//用来记录e的次数
while (index != -1)
{
    
    
	count++;
	index = str.IndexOf('e', index + 1);
	//indexof中是从int类型的开始查找,所以要从'e'的
	//下一个字符开始
	if (index == -1)
	{
    
    
		break;
	}
Console.WriteLine("第{0}次出现e的位置是:{1}", count, index);
}

6. Enter a string, if evil appears in the string, replace it with **

string str = "余周周很邪恶,超级邪恶";
if (str.Contains("邪恶"))
{
    
    
	str = str.Replace("邪恶", "**");
}
Console.WriteLine(str);

The focus of this question is the usage of replace().


7. Change the elements in the array {"Lin Yang", "Yu Zhouzhou", "Yan Mo", "Zhao Qiaoyi"} into Lin Yang|Yu Zhouzhou|Yanmo|Zhao Qiaoyi

string[] str = new string[] {
    
     "林杨", "余周周", "言默", "赵乔一" };
string s = string.Join("|", str);
Console.WriteLine(s);
str = s.Split('|');
for (int i = 0; i < str.Length; i++)
{
    
    
	Console.Write(str[i]);
}

Summary:
There are many functional operations of strings, so more practice is needed (the sun is far away, but there must be a sun ).
There are two points that are easy to make mistakes:
①The s.Substring(0, 3);
first represents the position of the element, and the second A represents the length rather than the position
str.IndexOf('t', 2)
and here 2 represents the query character't' from position 2.

Guess you like

Origin blog.csdn.net/jhy1103/article/details/109323528