string关键字是System.String类的别名。String类具有以下两个属性-
| 序号 | 属性和说明 |
|---|---|
| 1 | Chars 在当前String对象的指定位置获取Char对象。 |
| 2 | Length 获取当前String对象中的字符数。 |
以下是String类的一些方法-
| 序号 | 方法与说明 |
|---|---|
| 1 | public static int Compare(string strA,string strB) 比较两个指定的字符串对象,并返回一个整数,该整数指示其在排序顺序中的相对位置。 |
| 2 | public static int Compare(string strA,string strB,bool ignoreCase) 比较两个指定的字符串对象,并返回一个整数,该整数指示排序顺序中它们的相对位置。但是,如果布尔参数为true,则忽略大小写。 |
| 3 | 公共静态字符串Concat(string str0,string str1) 连接两个字符串对象。 |
| 4 | 公共静态字符串Concat(字符串str0,字符串str1,字符串str2) 连接三个字符串对象。 |
| 5 | 公共静态字符串Concat(字符串str0,字符串str1,字符串str2,字符串str3) 连接四个字符串对象。 |
在下面的示例中,让我们看看如何创建字符串和连接字符串-
using System;
namespace StringApplication {
class StringProg {
static void Main(string[] args) {
string[] starray = new string[]{"Cricket is my life",
"It is played between two teams",
"It has three formats",
"T20, Test Cricket and ODI",
"Cricket is life"
};
string str = String.Join("\n", starray);
Console.WriteLine(str);
}
}
}