Csharp 简明教程

C

在 C# 中,可以使用字符串作为字符数组,然而,更常见的做法是使用 string 关键字来声明字符串变量。字符串关键字是 System.String 类的别名。

Creating a String Object

可以使用以下方法之一创建字符串对象 -

  1. 通过将字符串文字符号赋值给 String 变量

  2. 通过使用 String 类构造函数

  3. 通过使用字符串连接运算符 (+)

  4. 通过检索属性或调用返回字符串的方法

  5. 通过调用格式化方法将值或对象转换成它的字符串表示形式

以下示例演示了这一点−

using System;

namespace StringApplication {

   class Program {

      static void Main(string[] args) {
         //from string literal and string concatenation
         string fname, lname;
         fname = "Rowan";
         lname = "Atkinson";

         char []letters= { 'H', 'e', 'l', 'l','o' };
         string [] sarray={ "Hello", "From", "Tutorials", "Point" };

         string fullname = fname + lname;
         Console.WriteLine("Full Name: {0}", fullname);

         //by using string constructor { 'H', 'e', 'l', 'l','o' };
         string greetings = new string(letters);
         Console.WriteLine("Greetings: {0}", greetings);

         //methods returning string { "Hello", "From", "Tutorials", "Point" };
         string message = String.Join(" ", sarray);
         Console.WriteLine("Message: {0}", message);

         //formatting method to convert a value
         DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
         string chat = String.Format("Message sent at {0:t} on {0:D}", waiting);
         Console.WriteLine("Message: {0}", chat);
      }
   }
}

编译并执行上述代码后,将产生以下结果 −

Full Name: RowanAtkinson
Greetings: Hello
Message: Hello From Tutorials Point
Message: Message sent at 5:58 PM on Wednesday, October 10, 2012

Properties of the String Class

String 类具有以下两个属性 -

Sr.No.

Property & Description

1

Chars 获取当前 String 对象中指定位置处的 Char 对象。

2

Length 获取当前 String 对象中的字符数。

Methods of the String Class

String 类有许多方法可帮助使用字符串对象。下表列出了一些最常用的方法 -

有关方法和字符串类构造函数的完整列表,请访问 MSDN 库。

Examples

以下示例演示了一些上面提到的方法 -

Comparing Strings

using System;

namespace StringApplication {

   class StringProg {

      static void Main(string[] args) {
         string str1 = "This is test";
         string str2 = "This is text";

         if (String.Compare(str1, str2) == 0) {
            Console.WriteLine(str1 + " and " + str2 +  " are equal.");
         } else {
            Console.WriteLine(str1 + " and " + str2 + " are not equal.");
         }
         Console.ReadKey() ;
      }
   }
}

编译并执行上述代码后,将产生以下结果 −

This is test and This is text are not equal.

String Contains String

using System;

namespace StringApplication {

   class StringProg {

      static void Main(string[] args) {
         string str = "This is test";

         if (str.Contains("test")) {
            Console.WriteLine("The sequence 'test' was found.");
         }
         Console.ReadKey() ;
      }
   }
}

编译并执行上述代码后,将产生以下结果 −

The sequence 'test' was found.

Getting a Substring

using System;

namespace StringApplication {

   class StringProg {

      static void Main(string[] args) {
         string str = "Last night I dreamt of San Pedro";
         Console.WriteLine(str);
         string substr = str.Substring(23);
         Console.WriteLine(substr);
      }
   }
}

编译并执行上述代码后,将产生以下结果 −

San Pedro

Joining Strings

using System;

namespace StringApplication {

   class StringProg {

      static void Main(string[] args) {
         string[] starray = new string[]{"Down the way nights are dark",
            "And the sun shines daily on the mountain top",
            "I took a trip on a sailing ship",
            "And when I reached Jamaica",
            "I made a stop"};

         string str = String.Join("\n", starray);
         Console.WriteLine(str);
      }
   }
}

编译并执行上述代码后,将产生以下结果 −

Down the way nights are dark
And the sun shines daily on the mountain top
I took a trip on a sailing ship
And when I reached Jamaica
I made a stop