C#基础-056 字符串练习题

2017-09-11 14:46:03来源:网络收集作者:咖啡不加糖人点击

分享

static void Main(string[] args)
{
///*1.去掉字符串中的所有空格*/
//string str = " saggasg sagsgssagaseg";
//Console.WriteLine(MainClass.Task01(str));
///*2.根据完整的路径从路径中分离文件路径、文件名及扩展名*/
//string pa = @"c:/1/2/3/hello world.txt";
//Console.WriteLine("文件名:{0}", Path.GetFileName(pa));
//Console.WriteLine("扩展名:{0}",Path.GetExtension(pa));
//Console.ReadKey();
///*3.获取字符串中汉字的个数*/
//str = "asbbf阿斯蒂芬笔记本sdggas466";
//Console.WriteLine(MainClass.Task03(str));
//Console.ReadKey();
///*4.对字符串进行加密与解密*/
//string userName = "452dfg比暗勃515较8";
//Console.WriteLine(MainClass.Task04(userName, 20));
//Console.ReadLine();
//Console.WriteLine(MainClass.Task04_01(MainClass.Task04(userName, 20), 20));
//Console.ReadKey();
///*5.将字母全部转换为大写或小写*/
//string testStr = "bnsdfBKSA6546as东方不败";
//Console.WriteLine(MainClass.Task05_MyToLower(testStr));
//Console.WriteLine(MainClass.Task05_MyToUpper(testStr));
//Console.ReadKey();
///*6.根据标点符号对字符串进行分行*/
//str = "按时间,暗示法 、大师傅,第三。但是发";
//str = MainClass.Task06(str);
//Console.WriteLine(str);
//Console.ReadKey();
///*7.去掉字符串数组中每个字符串的空格*/
//string[] strArr = {"cfhr "," werwr et ewt twert ","ewf fag" };
//strArr=MainClass.Task07(strArr);
//ShowStr(strArr);
//Console.WriteLine();
//Console.ReadKey();
///*8.随意输入你心中想到的一个书名,然后输出它的字符串长度。(Length属性:可以得字符串的长度)*/
//Console.WriteLine("请输入一个书名");
//string bookName = Console.ReadLine();
//Console.WriteLine("这本书的名字长度为:{0}", MainClass.Task08(bookName));
///*9.两个学员输入各自最喜欢的游戏名称,判断是否一致,如果相等,则输出你们俩喜欢相同的游戏;如果不相同,则输出你们俩喜欢不相同的游戏。*/
//Console.WriteLine("请输入你的最喜欢的游戏名称");
//string gameName1 = Console.ReadLine();
//Console.WriteLine("请输入你的最喜欢的游戏名称");
//string gameName2 = Console.ReadLine();
//MainClass.Task09(gameName1,gameName2);
//Console.ReadKey();
///*10.上题中两位同学输入 lol和 LOL代表同一游戏,怎么办?*/
//Console.WriteLine("请输入你的最喜欢的游戏名称");
//string name1 = Console.ReadLine();
//Console.WriteLine("请输入你的最喜欢的游戏名称");
//string name2 = Console.ReadLine();
//MainClass.Task10(name1, name2);//同时转化成大写或者小写在进行比较
//Console.ReadKey();
///*11.让用户输入一个日期格式如“2008/08/08”,将输入的日期格式转换为“2008年-8月-8日”。*/
//string date = " 2017 /11 / 08 ";
//Console.WriteLine(MainClass.Task11(date));
///*12.接收用户输入的字符串,将其中的字符进行排序(升序),并以逆序的顺序输出,“cabed”→"abcde"→“edcba”。*/
//string str = "cabed";
//Console.WriteLine(MainClass.Task12(str));
///*13.接收用户输入的一句英文,将其中的单词以反序输出,“hello c sharp”→“sharp c hello”。*/
//string str = "hello c sharp";
//Console.WriteLine(MainClass.Task13(str));
///*14.从请求地址中提取出用户名和域名:http://www.163.com?userName=admin&pwd=123456*/
//string domain = null;
//string username = null;
//MainClass.Task14("http://www.163.com?userName=admin&pwd=123456", out username, out domain);
//Console.WriteLine("域名为:{0},用户名为:{1}", domain, username);
///*15.有个字符串数组,存储了10个书名,书名有长有短,现在将他们统一处理,若书名长度大于10,则截取长度8的子串并且最后添加“...”,加一个竖线后输出作者的名字。*/
//string[] strArr = new string[10];
//string[] autorArr = new string[10];
//strArr[0] = "平凡的世界";
//strArr[1] = "穆斯林的葬礼";
//strArr[2] = "挪威的森林";
//strArr[3] = "基督山伯爵";
//strArr[4] = "教父";
//strArr[5] = "苏菲的世界";
//strArr[6] = "白鹿原";
//strArr[7] = "建设有中国特色的社会主义民主共和国";
//strArr[8] = "我爱祖国大地的美好山川和我可爱的家乡";
//strArr[9] = "关于物理学家彼得罗夫在圣彼得堡外科医学院借助有时由4200个铜环与锌环构成的巨大电池组所作的伽伐尼——伏打实验的消息";
//autorArr[0] = "路遥";
//autorArr[1] = "霍达";
//autorArr[2] = "村上春树";
//autorArr[3] = "大仲马";
//autorArr[4] = "马里奥·普佐";
//autorArr[5] = "乔斯坦·贾德";
//autorArr[6] = "陈忠实";
//autorArr[7] = "时建";
//autorArr[8] = "老四";
//autorArr[9] = "彼得罗夫";
//ShowStr(MainClass.Task15(strArr,autorArr));
///*16.让用户输入一句话,找出所有"呵"的位置。*/
//string str = "萨芬和呵啥地方基本框架呵家世界呵撒 暗示法三方 ";
//MainClass.Task16(str,'呵');
///*17.让用户输入一句话,找出所有"呵呵"的位置。*/
//string str = "萨芬和呵呵啥地方基本呵呵架呵呵家世呵呵呵呵呵呵界呵撒 暗示法呵呵三方 ";
//ShowList(MainClass.Task17(str, "呵呵呵"));
///*18.让用户输入一句话,判断这句话中有没有邪恶,如果有邪恶就替换成这种形式然后输出,如:“老牛很邪恶”,输出后变成”老牛很**”;*/
//string str = "老牛很邪恶";
//Console.WriteLine(MainClass.Task18(str,"邪恶"));
///*19.如何判断一个字符串是否为另一个字符串的子串*/
//string str1 = "sagasghgfhhrwj5";
//string str2 = "sagasghgfhhrwj5agvhg5a";
//Console.WriteLine(MainClass.Task19(str1,str2));
///*20.如何验证一个字符串中的每一个字符均在另一个字符串中出现过*/
//string str1 = "saghw";
//string str2 = "sage";
//Console.WriteLine(MainClass.Task20_01(str1, str2));
///*21.如何随机生成无数字的全字母的字符串*/
//Console.WriteLine(MainClass.Task21(10));//随机字母位数
///*22.如何随机生成带数字和字母的字符串*/
//Console.WriteLine(MainClass.Task22(10));//随机字母或数字位数
///*23.如何判定一个字符串中既有数字又有字母*/
//string str = "1548tbvj但是不会";
//Console.WriteLine(MainClass.Task23(str));
///*24.字符串内的字符排序(只按字母序不论大小写)*/
//string str = "DdbDHGBaGHgbhA";
//Console.WriteLine(MainClass.Task24(str));
///*25.字符串的补位操作
// 1=》001
// 2=》002
// 10 =》010*/
//int number = 100;
//MainClass.Task25(number);
//Console.WriteLine(str.PadLeft(2, '0'));//向左补位到共两位,补的字符是‘0’
//Console.WriteLine(str.PadRight(4, '0'));//向右补位到共四位,补的字符是‘0’
///*26.”DdBaGHgbhA”---? “abdghABDGH”-*/
//string str = "DdBaGHgbhA";
//Console.WriteLine(MainClass.Task26(str));
}
static void ShowStr(string[] strArr)
{
for (int i = 0; i < strArr.Length; i++)
{
Console.WriteLine(strArr[i] + " ");
}
Console.WriteLine();
}
static void ShowIntArr(int[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i]+" ");
}
Console.WriteLine();
}
static void ShowList(HashSet hs)
{
foreach (var item in hs)
{
Console.WriteLine(item);
}
}
class MainClass
{
public static string Task01(string str)
{
string retStr = string.Empty;
for (int i = 0; i < str.Length; i++)
{
if (str[i] != ' ')
{
retStr = retStr + str[i];
}
}
return retStr;
}
public static int Task03(string str)
{
int count = 0;
Regex regex = new Regex(@"^[/u4E00-/u9FA5]{0,}$");
for (int i = 0; i < str.Length; i++)
{
if (regex.IsMatch(str[i].ToString()))
{
count++;
}
}
return count;
}
public static string Task04(string str, int number)//加密
{
string retStr = string.Empty;
for (int i = 0; i < str.Length; i++)
{
retStr += (char)(str[i] + number) + " ";
}
return retStr;
}
public static string Task04_01(string str, int number)//破译
{
string retStr = string.Empty;
for (int i = 0; i < str.Length; i++)
{
if (str[i] != ' ')
{
retStr += (char)(str[i] - number) + " ";
}
}
return retStr;
}
public static string Task05_MyToUpper(string str)
{
string retStr = string.Empty;
for (int i = 0; i < str.Length; i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
retStr += char.ToUpper(str[i]);
continue;
}
retStr += str[i];
}
return retStr;
}
public static string Task05_MyToLower(string str)
{
string retStr = string.Empty;
for (int i = 0; i < str.Length; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
retStr += char.ToLower(str[i]);
continue;
}
retStr += str[i];
}
return retStr;
}
public static string Task06(string str)
{
string retStr = string.Empty;
for (int i = 0; i < str.Length; i++)
{
if (str[i] == ',' || str[i] == '、' || str[i] == '。')
{
retStr += '/n';
continue;
}
retStr += str[i];
}
return retStr;
}
public static string[] Task07(string[] strArr)
{
string[] retStr = new string[strArr.Length];
for (int i = 0; i < strArr.Length; i++)
{
retStr[i] = MainClass.Task01(strArr[i]);
}
return retStr;
}
public static int Task08(string str)
{
return str.Length;
}
public static void Task09(string str1, string str2)
{
if (str1 == str2)
{
Console.WriteLine("你们俩喜欢相同的游戏");
}
else
{
Console.WriteLine("你们俩喜欢不相同的游戏");
}
}
public static void Task10(string str1, string str2)//不区分大小写
{
if (Task05_MyToLower(str1) == Task05_MyToLower(str2))
{
Console.WriteLine("你们俩喜欢相同的游戏");
}
else
{
Console.WriteLine("你们俩喜欢不相同的游戏");
}
}
public static string Task11(string str)//此类问题优先选择字符拼接,字符(串)替换会影响原来字符串的长度,有可能会溢出
{
str = Task01(str);
int count = 1;
string retDate = string.Empty;
for (int i = 0; i < str.Length; i++)
{
if (str[i] == '/' && count == 1)
{
if (str[i + 1] == '0')
{
retDate += "年-";
count++;
i++;
}
else
{
retDate += "年-";
count++;
}
continue;
}
if (str[i] == '/' && count == 2)
{
if (str[i + 1] == '0')
{
retDate += "月-";
i++;
}
else
{
retDate += "月-";
}
continue;
}
retDate += str[i];
}
retDate += '日';
return retDate;
}
public static string Task12(string str)
{
char[] chArr = new char[str.Length];
chArr = str.ToCharArray();
for (int i = 0; i < chArr.Length - 1; i++)//冒泡算法,外层表示趟数,内层表示每趟需要交换的次数
{
for (int j = 0; j < chArr.Length - i - 1; j++)
{
if (chArr[j] > chArr[j + 1])
{
char temp = ' ';
temp = chArr[j];
chArr[j] = chArr[j + 1];
chArr[j + 1] = temp;
}
}
}
string ret = string.Empty;
for (int i = 0; i < chArr.Length; i++)
{
ret = chArr[i] + ret;
}
return ret;
}
public static string Task13(string str)
{
string[] words = str.Split(' ','&','^');//把字符串按“ ”进行分割,放到字符串数组
//Console.WriteLine(words.Length);
string retStr = string.Empty;
//输出方法一
//for (int i = words.Length - 1; i >= 0; i--)//倒序输出字符串数组中的元素
//{
//retStr += words[i] + ' ';
//}
Array.Reverse(words);
//输出方法二
//for (int i = 0; i < words.Length; i++)
//{
//retStr += words[i]+" ";
//}
//输出方法三
foreach (var item in words)
{
retStr += item + " ";
}
return retStr;
}
public static void Task14(string url, out string username, out string domain)
{
username = "";
domain = "";
string[] tmpArrD = url.Split('.', '?');
domain = tmpArrD[1] + '.' + tmpArrD[2];
string[] tmpArrU = url.Split('=', '&');
username = tmpArrU[1];
}
public static string[] Task15(string[] nameArr, string[] autorArr)
{
for (int i = 0; i < nameArr.Length; i++)
{
if (nameArr[i].Length > 10)
{
nameArr[i] = nameArr[i].Substring(0, 8) + "..." + "|" + autorArr[i];
continue;
}
nameArr[i] = nameArr[i] + "|" + autorArr[i];
}
return nameArr;
}
public static void Task16(string str, char ch)
{
for (int i = 0; i < str.Length; i++)
{
if (str[i] == ch)
{
Console.Write(i + 1 + " ");
}
}
Console.WriteLine();
}
public static HashSet Task17(string str1, string str2)
{
int count = 0;
for (int i = 0; i < str1.Length - str2.Length + 1; i++)
{
for (int j = 0; j < str2.Length; j++)
{
if (str2[j] != str1[i + j])
{
break;
}
if (j == str2.Length - 1)
{
count++;
i = i + str2.Length - 1;
break;
}
}
}
//Console.WriteLine(count);
HashSet hs = new HashSet();
int temp = 0;
for (int i = 1; i <= count; i++)
{
temp = str1.IndexOf(str2, temp + str2.Length);
hs.Add(temp);
}
return hs;
}
public static string Task18(string str, string str2)
{
str = str.Replace(str2, "**");
return str;
}
public static bool Task19(string str1, string str2)
{
if (str1.Length < str2.Length)
{
string tmp = string.Empty;
tmp = str1;
str1 = str2;
str2 = tmp;
}
for (int i = 0; i < str1.Length - str2.Length + 1; i++)
{
for (int j = 0; j < str2.Length; j++)
{
if (str2[j] != str1[i + j])
{
break;
}
if (j == str2.Length - 1)
{
return true;
}
}
}
return false;
}
public static bool Task20(string str1, string str2)
{
if (str1.Length < str2.Length)
{
string tmp = string.Empty;
tmp = str1;
str1 = str2;
str2 = tmp;
}
HashSet chhs = new HashSet();
for (int i = 0; i < str2.Length; i++)
{
chhs.Add(str2[i]);
}
foreach (var item in chhs)
{
if (str1.Contains(item) == false)
{
return false;
}
}
return true;
}
public static bool Task20_01(string str1, string str2)
{
for (int i = 0; i < str2.Length; i++)
{
if (str1.Contains(str2[i]) == false)
{
return false;
}
}
return true;
}
public static string Task21(int length)
{
Random random = new Random();
string str = string.Empty;
while (str.Length < length)
{
int temp = random.Next(1, 200);
if (temp >= 'A' && temp <= 'Z' || temp >= 'a' && temp <= 'z')
{
str += (char)temp;
}
}
return str;
}
public static string Task22(int length)
{
Random random = new Random();
string str = string.Empty;
while (str.Length < length)
{
int temp = random.Next(1, 200);
if (temp >= 'A' && temp <= 'Z' || temp >= 'a' && temp <= 'z' || temp >= '0' && temp <= '9')
{
str += (char)temp;
}
}
return str;
}
public static bool Task23(string str)
{
for (int i = 0; i < str.Length; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z' || str[i] >= 'a' && str[i] <= 'z')
{
for (int j = 0; j < str.Length; j++)
{
if (str[j] >= '0' && str[j] <= '9')
{
return true;
}
}
}
}
return false;
}
public static string Task24(string str)
{
string bigWords = string.Empty;
string smallWords = string.Empty;
for (int i = 0; i < str.Length; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
bigWords += str[i];
}
if (str[i] >= 'a' && str[i] <= 'z')
{
smallWords += str[i];
}
}
//Console.WriteLine("大写:{0}",bigWords.Length);
//Console.WriteLine("小写:{0}", smallWords.Length);
string temp = Task05_MyToLower(str);
char[] chArr = temp.ToCharArray();
Array.Sort(chArr);
string retStr = string.Empty;
for (int i = 0; i < chArr.Length; i++)
{
if (bigWords.Contains((char)(chArr[i] - 'a' + 'A')))
{
retStr += (char)(chArr[i] - 'a' + 'A');
bigWords = bigWords.Remove(bigWords.IndexOf((char)(chArr[i] - 'a' + 'A')),1);
continue;
}
retStr += chArr[i];
}
return retStr;
}
public static void Task25(int endNumber)
{
for (int i = 1; i <= endNumber; i++)
{
string str = i.ToString();
if (str.Length / i == 0 || str.Length / i == 1)
{
str = str.PadLeft(str.Length + 1, '0');
}
Console.WriteLine(str);
}
}
public static string Task26(string str)
{
char[] chArr = str.ToCharArray();
string bigWords = string.Empty;
string smallWords = string.Empty;
for (int i = 0; i < chArr.Length; i++)
{
if (chArr[i]>='a'&&chArr[i]<='z')
{
smallWords += chArr[i];
}
if (chArr[i] >= 'A' && chArr[i] <= 'Z')
{
bigWords += chArr[i];
}
}
char[] chBigArr = bigWords.ToCharArray();
char[] chSmallArr = smallWords.ToCharArray();
Array.Sort(chSmallArr);
Array.Sort(chBigArr);
smallWords = new string(chSmallArr);
bigWords = new string(chBigArr);
return smallWords+bigWords;
}
}


最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台