1. using System;
    2. using System.IO;
    3. using System.Text;
    4. namespace _058_字符串的练习
    5. {
    6. class Program
    7. {
    8. static void Main(string[] args)
    9. {
    10. //1、"abcdefg"->"gfedcba"
    11. string s = "abcdefg";
    12. char[] chs = s.ToCharArray();
    13. for (int i = 0; i < chs.Length / 2; i++)
    14. {
    15. char temp = chs[i];
    16. chs[i] = chs[chs.Length - i - 1];
    17. chs[chs.Length - i - 1] = temp;
    18. }
    19. s = new string(chs);
    20. Console.WriteLine(s);
    21. //2、"Hello C Sharp"->"Sharp C Hellp"
    22. string s1 = "Hello C Sharp";
    23. string[] str = s1.Split(" ", StringSplitOptions.RemoveEmptyEntries);
    24. for (int i = 2; i >=0 ; i--)
    25. {
    26. Console.Write(str[i]+" ");
    27. }
    28. Console.WriteLine();
    29. //s1 = string.Join(" ", s1);
    30. //3、从Email中提取用户名和域名:abc@163.com
    31. string email = "abc@163.com";
    32. string[] str1 = email.Split("@", StringSplitOptions.RemoveEmptyEntries);
    33. string userName = str1[0];
    34. string domainName = str1[1];
    35. //int index = email.IndexOf('@');
    36. //string userName = email.Substring(0, index);
    37. //string domainName = email.Substring(index+1);
    38. Console.WriteLine("用户名:{0}\n域名:{1}",userName,domainName);
    39. //4、读取文本
    40. string path = @"E:\cSharptext1.txt";
    41. string[] contents = File.ReadAllLines(path, Encoding.Default);
    42. for (int i = 0; i < contents.Length; i++)
    43. {
    44. string[] strNew = contents[i].Split(" ", StringSplitOptions.RemoveEmptyEntries);
    45. Console.WriteLine((strNew[0].Length>7?strNew[0].Substring(0,7)+"...":strNew[0]) + '|' + strNew[1]);
    46. }
    47. //5、用户输入数据,找出所有“ ”的位置
    48. string userData = Console.ReadLine();
    49. //int Count1 = 0;
    50. //for (int i = 0; i < userData.Length; i++)
    51. //{
    52. // if (userData[i]=='我')
    53. // {
    54. // Count1++;
    55. // Console.WriteLine("第{0}次出现的位置是{1}", Count1, i);
    56. // }
    57. //}
    58. int index = userData.IndexOf('我');
    59. int count = 1;//记录出现次数
    60. Console.WriteLine("第{0}次出现的位置是{1}", count, index);
    61. //循环体:从上一次出现' '的位置+1的位置找下一次出现' '的位置
    62. //循环条件:index!=-1
    63. while (index!=-1)
    64. {
    65. count++;
    66. index = userData.IndexOf('我', index + 1);
    67. if (index == -1)
    68. {
    69. break;
    70. }
    71. Console.WriteLine("第{0}次出现的位置是{1}",count,index);
    72. }
    73. //6、"老牛很邪恶"->"老牛很**"
    74. string s3 = "老牛很邪恶";
    75. if (s3.Contains("邪恶"))
    76. {
    77. s3 = s3.Replace("邪恶", "**");
    78. }
    79. Console.WriteLine(s3);
    80. Console.ReadKey();
    81. }
    82. }
    83. }