[C#] C# 字符串方法和属性的演示代码 →→→→→进入此内容的聊天室

来自 , 2019-10-06, 写在 C#, 查看 150 次.
URL http://www.code666.cn/view/8710ef76
  1. using System;
  2.  
  3. class StringMethods
  4. {
  5.    static void Main()
  6.    {
  7.       char[] characterArray;
  8.       int position;
  9.       string result, string1;
  10.  
  11.       string1 = "The education of Cissy!";
  12.       characterArray = new char[30];
  13.  
  14.       // create the output string
  15.       result = "string = \"" + string1 + "\"";
  16.  
  17.       // Length property
  18.       result += "\nstring length = " + string1.Length;
  19.  
  20.       // IndexOf method (returns -1 if not found)
  21.       position = string1.IndexOf ("e");
  22.       result += "\nstring contains an 'e' at index: "
  23.          + position;
  24.  
  25.       // find another "e"
  26.       position = string1.IndexOf ("e", position + 1);
  27.       result += "\nstring contains a second 'e' at index: "
  28.          + position;
  29.  
  30.       // Search/Find a substring (returns True or False)
  31.       if (string1.Contains ("Cissy"))
  32.       result += "\nstring contains 'Cissy' --> "
  33.          + string1.Contains ("Cissy");
  34.  
  35.       // Search/Find a substring position
  36.       position = string1.IndexOf("Cissy");
  37.       result += "\n'Cissy' starts at index: "
  38.          + position;
  39.  
  40.       // change case of string
  41.       result += "\nlower case string: \""
  42.          + string1.ToLower() + "\"";
  43.       result += "\nupper case string: \""
  44.          + string1.ToUpper() + "\"";
  45.  
  46.       // indexing, loop through characters
  47.       // in string1 and display in reverse order
  48.       result += "\nreverse string: \"";
  49.       for (int i = string1.Length - 1; i >= 0; i--)
  50.       {
  51.          result += string1[i];
  52.       }
  53.  
  54.       // Replace method
  55.       result += "\"\nreplace 'educ' with 'matur': ";
  56.       result += "\"" + string1.Replace ("educ", "matur") + "\"";
  57.  
  58.       // Use the CopyTo method to copy characters
  59.       // from string1 into characterArray
  60.       string1.CopyTo (0, characterArray, 0, 6);
  61.       result += "\nFirst 6 characters of array contain: \"";
  62.       // display array
  63.       for (int i = 0; i < 6; i++)
  64.       {
  65.          result += characterArray[i];
  66.       }
  67.       Console.WriteLine (result +
  68.          "\"\n\n(Press \"Enter\" to exit.)");
  69.       Console.Read();
  70.    }
  71. }
  72. //csharp/4158

回复 "C# 字符串方法和属性的演示代码"

这儿你可以回复上面这条便签

captcha