C#字符串习题练习

C#字符串习题练习

前言:
C#学的愈发迷惑,今天抽出一点时间来敲了敲一些基础的代码题,加深对字符串的印象。啊啊啊啊啊,不过写的真的好慢,以后还要提高效率。话不多说,切入正题。


1.将字符串按如下格式输出: “abc” —> “cba”

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

输出结果:cba(但需要指出的是,这只是一种投机取巧的方法而已)

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);

这里要知道StringBuilder中append()相较于string str中 ‘+’ 的操作的区别,前者是在原有基础上进行添加,而后者是不停的开辟新的空间.


2.将字符串 “hello c sharp” 输出为 “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);

这道题是将单词倒叙而不再仅仅是对单个字母,而这也是这道题的一个难点.


3.用户输入一个邮箱账号,提取用户名和域名(@不属于任何一个)

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();

这道题我们首先用indexof找到‘@’位置,然后输出位置0-’@‘前一个字符,通过遍历输出;然后从’@'的后一个字符开始遍历到最后并进行输出.


4.有若干本图书,每一行有书名和作者名,中间用一个空格隔开。输出书名,书名的最大长度为10,若书名长度超过10,则输出前8个字符,并加上"…";书名和作者名间的空格去掉加上’|’.

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]);
}

创建一个string类型的数组,然后遍历每一个每一个元素.对于str中的每个元素,其都相当于string类型的字符串。在for循环中,我们采用split方法,把一个字符串变成字符数组,此时如果输出 “strNew.Length“ 那么答案是2.因为strNew这个数组中存储着两个元素,一个是书名,一个是作者名.然后在输出语句中进行书名长度的判断.


5.让用户输入一句话,查询其中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.输入一个字符串,若字符串中出现邪恶,则把它替换为**

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

这道题的考察点是replace()的用法.


7.将数组中的元素{“林杨”,“余周周”,“言默”,“赵乔一”} 变成 林杨|余周周|言默|赵乔一

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]);
}

总结:
字符串的函数操作有很多,所以还需要多加练习才可以(太阳尚远,但必有太阳).
其中有两个容易搞错的点:
s.Substring(0, 3);
第一个代表元素的位置,而第二个代表长度而不是位置
str.IndexOf('t', 2)
而这里的2代表着从位置2开始查询字符’t’.

猜你喜欢

转载自blog.csdn.net/jhy1103/article/details/109323528