字符串的应用:
1)字符串的比较
Compare(str1, str2)
str1.CompareTo(str2)
string a = "hello",b="Hello",c="hello";
Console.WriteLine(a.CompareTo(b));//-1
Console.WriteLine(string.Compare(a,b));//-1
Console.WriteLine(a.CompareTo(c));//0
- 字符串的查找
(1)Contains(Findstr):找指定字符串是否包含一个字串Findstr,返回值的bool类型,即只有
true和false。
(2)、IndexOf(Findstr):查找FindStr在字符串中第一次出现的位置,返回值为第一次出现
的下标,没有找到则返回-1.
(3)、LastIndexOf(FindStr):查找FindStr在字符串中最后一次出现的位置,返回值为最后
一次出现的下标,没有找到则返回-1。
Console.WriteLine(a.Contains("lo"));//true
Console.WriteLine(a.IndexOf('e'));//1
Console.WriteLine(a.LastIndexOf('l'));//3
- 字符串的截取
SubString(StartIndex):字符串中下标从StartIndex开始后面的全部字符串。
SubString(StartIndex, Len):字符串中下标从StartIndex开始后面的Len个长度的字符串。
Console.WriteLine(a.Substring(1));//ello
Console.WriteLine(a.Substring(1,3));//ell
- 字符串的分割
Split(SplitCh):将字符串按SplitCh进行分割,它的返回值是一个字符串数组。
string d="锄禾日当午#汗滴禾下土#谁知盘中餐#粒粒皆辛苦";
string[] e = d.Split('#');
for (int i = 0; i < e.Length; i++)
{
Console.WriteLine(e[i]);
}
- 字符串的合并
string.Concat(str1, str2, …., strn):将n个字符串连接,中间没有连接符
“+”连接符号
string f = "world";
Console.WriteLine(a+f);
Console.WriteLine(1+2+3+a);//6hello
Console.WriteLine(a+1+2+3);//hello123
Console.WriteLine(string.Concat(a,f));//helloworld
- 字符串的替换
Replace(oldStr, newStr):用newStr来替换字符串中的oldStr
Console.WriteLine(a.Replace("he","HE"));//HEllo
- 字符串的插入
Insert(index, str):index是需要插入的位置,str是要插入的字符
Console.WriteLine(a.Insert(1,"ww"));//hwwello
抽象类的定义:
计算器类(父类)里面有加减乘除四个功能都实现了,但是很子类继承后,有些方法都自己重写了,这个时候父类里面的这些方法功能就没有意义了。所以有些方法不需要都实现了,只需要声明即可(抽象方法)。
抽象类(what):类是一个具有相同特征和行为的抽象,而抽象类(class 前加上 abstract)没有指出行为的具体细节,而由他的子类去实现相应的行为。
把普通类前面加一个abstract关键字就是抽象类。
含有一个或多个抽象方法的类