在 c# 中,使用字符串作为字符数组,但更常见的做法是使用string
关键字来声明一个字符串变量。string
关键字是system.string
类的别名。
可以使用以下方法之一来创建字符串对象:
string
变量string
类构造函数+
)示例:
using system;
namespace stringapplication
{
class program
{
static void main(string[] args)
{
//from string literal and string concatenation
string fname, lname;
fname = "rowan";
lname = "atkinson";
string fullname = fname + lname;
console.writeline("full name: {0}", fullname);
//by using string constructor
char[] letters = { 'h', 'e', 'l', 'l','o' };
string greetings = new string(letters);
console.writeline("greetings: {0}", greetings);
//methods returning string
string[] sarray = { "hello", "from", "h3", "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: rowan atkinson
greetings: hello
message: hello from h3 point
message: message sent at 5:58 pm on wednesday, october 10, 2012
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 ) | 比较两个指定的字符串对象,并返回一个整数,指示其在排序顺序中的相对位置。 但是,如果布尔参数为真,它将忽略大小写。 |
3 | public static string concat(string str0, string str1) | 连接两个字符串对象。 |
4 | public static string concat(string str0, string str1, string str2) | 连接三个字符串对象。 |
5 | public static string concat(string str0, string str1, string str2, string str3) | 连接四个字符串对象。 |
6 | public bool contains(string value) | 返回一个值,该值指示在此字符串中是否发生指定的string 对象。 |
7 | public static string copy(string str) | 创建与指定字符串相同值的新string 对象。 |
8 | public void copyto(int sourceindex, char[] destination, int destinationindex, int count) | 将指定数量的字符从string 对象的指定位置复制到unicode字符数组中的指定位置。 |
9 | public bool endswith(string value) | 确定字符串对象的末尾是否与指定的字符串匹配。 |
10 | public bool equals(string value) | 确定当前的string 对象和指定的string 对象是否具有相同的值。 |
11 | public static bool equals(string a, string b) | 确定两个指定的string 对象是否具有相同的值。 |
12 | public static string format(string format, object arg0) | 用指定对象的字符串表示替换指定字符串中的一个或多个格式项。 |
13 | public int indexof(char value) | 返回当前字符串中指定unicode字符首次出现的从零开始的索引。 |
14 | public int indexof(string value) | 返回此实例中指定字符串第一次出现的从零开始的索引。 |
15 | public int indexof(char value, int startindex) | 返回此字符串中指定unicode字符第一次出现的从零开始的索引,在指定的字符位置开始搜索。 |
16 | public int indexof(string value, int startindex) | 返回此实例中指定字符串第一次出现的从零开始的索引,在指定的字符位置开始搜索。 |
17 | public int indexofany(char[] anyof) | 返回指定unicode字符数组中任何字符在此实例中第一次出现的基于零的索引。 |
18 | public int indexofany(char[] anyof, int startindex) | 返回在指定的unicode字符数组中的任何字符在这个实例的第一次出现的基于零的索引,在指定的字符位置开始搜索。 |
19 | public string insert(int startindex, string value) | 返回一个新的字符串,在当前字符串对象的指定索引位置插入指定的字符串。 |
20 | public static bool isnullorempty(string value) | 判断指定的字符串是空值还是空字符串。 |
21 | public static string join(string separator, params string[] value) | 每个元素之间使用指定的分隔符来连接字符串数组的所有元素。 |
22 | public static string join(string separator, string[] value, int startindex, int count) | 每个元素之间使用指定的分隔符来连接字符串数组的指定元素。 |
23 | public int lastindexof(char value) | 返回当前字符串对象中指定unicode字符,从零开始搜索的最后一次出现的索引位置。 |
24 | public int lastindexof(string value) | 返回当前字符串对象中指定字符串从零开始搜索的最后一次出现的索引位置。 |
25 | public string remove(int startindex) | 删除当前实例中从指定位置开始到最后一个位置的所有字符,并返回此字符串。 |
26 | public string remove(int startindex, int count) | 从指定位置移除当前字符串中指定数量的字符,并返回字符串。 |
27 | public string replace(char oldchar, char newchar) | 使用指定的unicode字符替换当前字符串对象中指定的unicode字符的所有出现字符,并返回新的字符串。 |
28 | public string replace(string oldvalue, string newvalue) | 使用指定的字符串替换当前字符串对象中指定字符串的所有出现字符,并返回新的字符串。 |
29 | public string[] split(params char[] separator) | 返回一个字符串数组,其中包含当前字符串对象中的子字符串,由指定的unicode字符数组的元素分隔。 |
30 | public string[] split(char[] separator, int count) | 返回一个字符串数组,其中包含当前字符串对象中的子字符串,由指定的unicode字符数组的元素分隔。 int 参数指定要返回的子字符串的最大数量。 |
31 | public bool startswith(string value) | 确定此字符串实例的开始是否与指定的字符串匹配。 |
32 | public char[] tochararray() | 返回一个unicode字符数组,其中包含当前字符串对象中的所有字符。 |
33 | public char[] tochararray(int startindex, int length) | 返回一个unicode字符数组,其中包含当前字符串对象中的所有字符(从指定的索引开始,直到指定的长度。) |
34 | public string tolower() | 返回一个转换为小写的字符串的副本。 |
35 | public string toupper() | 返回一个转换为大写的字符串的副本。 |
36 | public string trim() | 从当前string对象中删除所有前导和尾随的空格字符。 |
您可以访问msdn库,获取方法和string类构造函数的完整列表。
以下示例演示了上述一些方法:
比较字符串:
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.
字符串包含字符串:
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.
获取子字符串:
using system;
namespace stringapplication
{
class stringprog
{
static void main(string[] args)
{
string str = "last night i dreamt of girls";
console.writeline(str);
string substr = str.substring(23);
console.writeline(substr);
}
}
}
当编译和执行上述代码时,会产生以下结果:
girls
将字符串连接:
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