一、字符串
常用函数
static void Main(string[] args)
{
string name="liLIfjeJIFJeif";
//变大写
Console.WriteLine(name.ToUpper());
//变小写
Console.WriteLine(name.ToLower());
string name2="jehfe";
//判断是否相同
Console.WriteLine(name.Equals(name2));
string address="中国|北京|海淀|上地";
//分割字符串
string[] addary=address.Split('|');
for(int i=0;i<addary.Length;i++){
Console.Write(addary[i]);
}
Console.WriteLine(name.Substring(2));//LIfjeJIFJeif
//从位置2之后两个
Console.WriteLine(name.Substring(2,2));//LI
//查找字符串在字符串中第一次出现的位置
Console.WriteLine(name.IndexOf("je"));//5
//查找字符串在字符串中最后一次出现的位置
Console.WriteLine(name.IndexOf("e"));//6
//判断是否以。。。开始
Console.WriteLine(name.StartsWith("heh"));
//判断是否以。。。结束
Console.WriteLine(name.EndsWith("if"));
//将旧的字符串替换成新的字符串
//liLIfkjeJIFJeifk
Console.WriteLine(name.Replace("f","fk"));
//判断是否包含
Console.WriteLine(name.Contains("je"));
//Trim去掉字符串中前后空格
//TrimEnd()去掉结束后的空格
//TrimStart()去掉开始前的空格
//liejf jifjle
Console.WriteLine(" liejf jifjle".Trim());
//判断字符串是否为空
Console.WriteLine(string.IsNullOrEmpty(""));
}
StringBuilder
using System.Text;
static void Main(string[] args)
{
StringBuilder sb=new StringBuilder();
//追加数据
sb.Append("mok");
sb.Append(123);
//转换为字符串
Console.WriteLine(sb.ToString());//mok123
//清空数据
sb.Clear();
}
二、枚举和结构体
枚举
public enum Week{
星期一,
星期二,
星期三,//注意是逗号
星期四,
星期五,
星期六,
星期日
}
class Program
{
static void Main(string[] args)
{
Week day=Week.星期三;
Console.WriteLine(day);//星期三
}
}
结构体
public enum gender{
boy,
girl
}
public struct Person{
public string name;
public int age;
public gender gender;
public string adress;
}
class Program
{
static void Main(string[] args)
{
Person lsq=new Person();
lsq.age=10;
lsq.name="lsq";
lsq.adress="北京";
lsq.gender=gender.girl;
}
}
三、面向对象
初识class
namespace test
{
class Apple{
public string color;
public int weight;
}
}
namespace test
{
class Program
{
static void Main(string[] args)
{
Apple h1=new Apple();
h1.color="红色";
h1.weight=200;
Console.WriteLine(h1.color);
}
}
}
属性
using System;
using System.Text;
namespace test
{
class Apple{
private string color;
private int weight;
public string Color{
get{return color;}
set{
if(value=="红色"||value=="绿色")
color=value;
else
color="红色";
}
}
}
}
using System;
using System.Text;
namespace test
{
class Program
{
static void Main(string[] args)
{
Apple h1=new Apple();
h1.Color="红色";
Console.WriteLine(h1.Color);
}
}
}
方法
//构造方法必须是public
public Apple(string color,int weight){
this.color=color;
this.weight=weight;
}
//析构方法
~Apple(){}
四、继承
构造方法
class Hero{
private string heroname;
private int attack;
public string Heroname{
get{return heroname;}
set{heroname=value;}
}
public int Attack{
get{return attack;}
set{
attack=value;
}
}
public Hero(){
}
public Hero(string heroname,int attack){
this.attack=attack;
this.heroname=heroname;
}
}
class LuKaSi :Hero{
public LuKaSi(){
}
public LuKaSi(string heroname,int attack)
:base(heroname,attack)
{
}
public void Shuisi(){
Console.WriteLine("我是技能");
}
public void Move(){
Console.WriteLine("我要移动");
}
}
class Program
{
static void Main(string[] args)
{
LuKaSi l1=new LuKaSi("lisf",45);
l1.Shuisi();
}
}
五、多态
虚方法
public class Shapes{
public int Y{get;private set;}
public int X{get;private set;}
public int Height{get; set;}
public int Width{get;set;}
public virtual void Draw(){
Console.WriteLine("执行基类的画图任务");
}
}
class Rectangle :Shapes{
public override void Draw()
{
Console.WriteLine("画一个圆形");
base.Draw();
}
}
class Circle:Shapes{
public override void Draw()
{
Console.WriteLine("画一个长方形");
base.Draw();
}
}
class Program
{
static void Main(string[] args)
{
var shapes=new List<Shapes>{
new Rectangle(),
new Circle()
};
foreach(var shape in shapes){
shape.Draw();
}
}
}