1、走马灯
static void Main(string[] args)
{
string str = "我是一个走马灯"; while (true) { Console.Clear(); string head = str[0].ToString(); string body = str.Substring(1); str = body + head; Console.WriteLine(str); System.Threading.Thread.Sleep(200); } }
2、字符串的插入、移除和替换
Insert string str = "012345"; str = str.Insert(3, "我"); Remove str = str.Remove(3,1); Replace string data = "2012年6月29日"; string str.Replace("原字符串", "新字符串"); string temp = data.Replace("年", "-").Replace("月","-").Replace("日","");
3、判断开始与结尾
比如检查是否为MP3歌曲 string path = @"D:\123\456\happy2000.wma"; if (path.EndsWith(".mp3")) { Console.WriteLine("是MP3"); } else { Console.WriteLine("不是mp3"); } path.StartsWith("")
4、接收用户输入的字符串,将其中的字符以与输入相反的顺序输出。"abc"→"cba"
Console.WriteLine("请输入一个字符串"); string str = Console.ReadLine(); str = DaoXu(str); Console.WriteLine(str); } private static string DaoXu(string str) { char[] chs = str.ToCharArray(); // 123456 for (int i = 0; i < chs.Length / 2; i++) { // i Length - i - 1 char temp = chs[i]; chs[i] = chs[chs.Length - 1 - i]; chs[chs.Length - 1 - i] = temp; } str = new string(chs); return str; #endregion
5、接收用户输入的一句英文,将其中的单词以反序输出。
例: “I love you"→“i evol uoy" static void Main(string[] args) { string strTemp = @"I love you"; strTemp = strTemp.ToLower(); string[] strs = strTemp.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < strs.Length; i++) { strs[i] = DaoXu(strs[i]); string temp = strs[i]; strs[i] = DaoXu(temp); } string s = string.Join(" ", strs); Console.WriteLine(s); }
1 static void Main(string[] args) 2 { 3 string str = "我是一个走马灯"; 4 while (true) 5 { 6 Console.Clear(); 7 string head = str[0].ToString(); 8 string body = str.Substring(1); 9 str = body + head; 10 Console.WriteLine(str); 11 System.Threading.Thread.Sleep(200); 12 } 13 }