C# 编程学习系列(八)字符串的使用

海氹有点甜 2021-12-16 14:37:38 浏览数 (2008)
反馈

前面的数据类型介绍中,有提到字符串是属于引用数据的一种,也是编程中最常用的数据类型之一。下面来详细学习记录一下关于字符串的内容。

1、创建字符串

字符串是引用数据类型,声明变量时,需要使用 string 关键字。

例如:

string str = "This is a string!";

值得注意的是,在 C# 中,字符串类型的变量一定是用双引号包裹的一串字符。而字符类型(char)使用单引号包裹起来的一个字符。

2、拼接字符串

在 C# 中拼接字符串的方法有很多,下面列举几种比较常见的拼接方式:

(1)使用拼接符号

可以直接使用 + 算术运算符,对字符串进行连接。

string s1 = "Hello";
string s2 = "World";
string s3 = s1 + s2;
Console.WriteLine(s3); // HelloWorld

字符串不仅是字符串之间可以拼接,而且可以和所有的数据类型进行拼接。这是因为所有数据类型都继承自 Object 这个基类,而 Object 中又有 toString 方法。因此,只要子类不重写这个方法,理论上来说,所有类型都可以转换为字符串类型。

string s = "asdf";
int i = 3;
float f = 3.2f;
bool b = true;
Console.WriteLine(s + i); // asdf3
Console.WriteLine(s + f); // asdf3.2
Console.WriteLine(s + b); // asdftrue

(2)string.Concat

调用 string.Concat() 方法,传入参数即可以完成拼接字符串。参数的个数至少是两个。

string s1 = "I ";
string s2 = "\'m ";
int i = 18;
string s = string.Concat(s1, s2, i);
Console.WriteLine(s); // I 'm 18

(3)占位符拼接

string name = "小明";
int age = 22;
string s =string.Format("{0}今年{1}岁", name, age);
Console.WriteLine(s);
// 小明今年22岁

(4)$方式拼接

C# 6.0开始出现的 $ 方式,也可以用来拼接字符串。实际上其实就是 string.Format 的简化版。

string name = "小刚";
int num = 3;
string s = $"{name}盖了{num}米高的楼层。";
Console.WriteLine(s);
// 小刚盖了3米高的楼层。

3、比较字符串

字符串也是可以进行比较的,比较方法有如下几种:

(1)使用比较运算符

string s1 = "Woooo";
string s2 = "Wooooa";
bool b = s1 == s2;
// false

(2)string.Compare

string.Compare(s1, s2) 对传入的两个字符串的各个字符进行比较,如果每个字符的 Unicode 编码值一样,那么将会返回 0;如果比较到某个字符的编码值不一样,前者大则立刻返回 1;否则就返回 -1.

string s1 = "abb";
string s2 = "abc";
string s3 = "caa";
string s4 = "abc";
int n1 = string.Compare(s1, s2); // -1
int n2 = string.Compare(s3, s1); // 1
int n3 = string.Compare(s2, s4); // 0

该方法,还可以传入第三个参数,布尔值类型。当布尔参数为 true 的时候,将不会区分大小写。

string s1 = "abc";
string s2 = "ABC";
int n1 = string.Compare(s1, s2); // -1
int n2 = string.Compare(s1, s2, true); // 0

(3) string.Equals

sting.Equals(s1, s2) 和上面的 string.Compare 很相似,不同的是前者返回的是 true 或者 false。

string s1 = "abc";
string s2 = "bbc";
string s3 = "aac";
string s4 = "abc";
bool b1 = string.Equals(s1, s2); // false
bool b2 = string.Equals(s1, s3); // false
bool b3 = string.Equals(s1, s4); // true

4、占位符

在上面已经有占位符的使用展示。

占位符,顾名思义,就是提前给你占位置,并标好标记。等人来,按照标记好的顺序坐下。

int n1 = 1;
int n2 = 2;
int n3 = 3;
Console.WriteLine("第{1}个位置,第{0}个位置,第{2}个位置", n1, n2, n3);
// 第2个位置,第1个位置,第3个位置

那么肯定会有这样的一种情况,来的路上突然有个人要加进来,但是位置提前安排了,结果不够坐,会发生什么事情?

int n1 = 1;
int n2 = 2;
int n3 = 3;
int n4 = 4;
Console.WriteLine("第{1}个位置,第{0}个位置,第{2}个位置", n1, n2, n3, n4);
// 第2个位置,第1个位置,第3个位置

很显然想要投机取巧的人,只配站着。

如果这个人不道德的话,不仅中途加入,还插在了第三个人前头,那么他成功了。

int n1 = 1;
int n2 = 2;
int n3 = 3;
int n4 = 4;
Console.WriteLine("第{1}个位置,第{0}个位置,第{2}个位置", n1, n2, n4, n3);
// 第2个位置,第1个位置,第4个位置

被插队的人就很遗憾只能站着了!

有时候主办方很聪明,料到有些人会不请自来,这时候会多安排一些个位置。可结果,人来少了,位置还多余了,会发生什么呢?

int n1 = 1;
int n2 = 2;
Console.WriteLine("第{1}个位置,第{0}个位置,第{2}个位置", n1, n2);

在这里插入图片描述

来的人就发现还有这么多位置没有人,看来这主办方不行啊,这台戏肯定不好看,散伙散伙!

从上面可以得出,位置可以少,但不能多;值可以多,但一定不能少,否则就会抛出以上这样的异常。

5、转义字符

转移符号 \

转义字符即转移符号加上特定的字符组成的一个特殊字符,可以在编程表示一些内容:

转义字符作用
\'在字符串中显示单引号
\"在字符串中显示双引号
\t制表符,相当于 tab 键
\n换行,相当于敲下了回车键
\\在字符串中显示斜杠符号
string s = "小明说:\"我很帅!\"\n小红说:\"\t\\yue\\\t\"";
Console.WriteLine(s);

在这里插入图片描述

6、总结

复盘一下上面提到的一些内容:

(1)字符串是引用数据类型,声明字符串变量时,需要使用 string。

(2)拼接字符串的方法有:拼接符号、Concat 方法、占位符使用、$方法使用

(3)比较字符串的方法有:比较运算符 ==、Compare 方法、Equals 方法。

(4)占位符:位置可少不可多,值可多不可少。

(5)常用转义字符的介绍。这里再提一句,转义字符是个字符,虽然它看着是用了两个字符组成的,但实际上它就只是一个字符。

关于字符串的学习记录,到这里就已经结束了。


0 人点赞