C#可视化四:异常和string类

本文主要介绍了C#中的异常处理机制,包括编译错误、逻辑错误和异常的区别,以及如何使用try...catch结构进行异常处理。同时,文章详细讲解了字符串处理,包括C#的内置字符串类型string,常用的方法如IndexOf、LastIndexOf、Substring等,并提供了实例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

知识点一:异常

程序错误的分类:
编译错误:
VisualStudio能够检测的到,编译通不过;
实例化非当前命名空间的类没有导入命名空间;变量没有定义就使用;局部局部变量没有初始化就使用;

逻辑错误:
VisualStudio检测没问题,编译没有问题,和实际要达到的效果不符合;
while循环没有终止条件;swith语句没有写break代码

异常:
VisualStudio检测没问题,编译能通过,逻辑也正确,但是在一些不可控的情况下出现的错误,并且会影响到程序的正常运行(程序会崩溃)
被除数为零,Command对象执行的sql语句错误,类型转换错误
异常处理,但是这些不可控的情况是可以预测的,针对这些情况,我们可以进行异常处理

针对类型转换,被除数为零这样的异常,出现的可能性太高了,并且可以从逻辑上先进行判断,没有必要进行try...catch;
但是针对数据库操作,文件操作这样的异常,逻辑上判断不可控,而且只在特定的情况下出现,就需要进行try...catch;

常见异常举例,数学异常;类型转换异常,数据库连接异常,文件操作异常

语法:
try ... catch结构
try ... catch ... finally 结构
try ... finally 结构

异常代码示例:

int num1 = 4;
int num2 = 0;
try
{
    Console.WriteLine(num1 / num2);
}
catch (DivideByZeroException e)
{
    Console.WriteLine(e.Message) ; //捕捉到异常
}
catch (Exception e)
{
    Console.WriteLine(e.Message); 
    //DivideByZeroException是继承值Exception类的,作用范围比Exception类要小
    //所以DivideByZeroException的catch要写在Exception之前;
    //Exception在前,DivideByZeroException在后,Exception会捕捉到所有异常,那么DivideByZeroException永远都不会捕捉到异常
    //同时Exception在前,DivideByZeroException在后,会报编译时异常,不能够通过编译
}
finally
{
    Console.WriteLine("finally 被执行了"); 
}

注意:
try 不能够省略,catch,finally可以省略
将可能出现异常的代码块写在try{}结构体中
try中的代码出现了异常就会被对应的异常的catch语句捕捉,进行处理(打印异常信息)
多个catch捕捉语句,如果异常有继承关系(父类的范围大于子类),需要注意顺序,先写catch范围小的异常
finally 无论异常是否发生,都会被执行(常用于数据库连接的关闭,文件流的关闭)

 

知识点二:字符串处理

C#常用数据类型,声明
八种常用的数据类型:
short, int, long, float, double, char, bool, string

整型:short, int, long(取值范围不一样)
浮点型:float, double
字符型:char
布尔类型:bool
引用类型: string

变量的声明与初始化: 
数据类型 变量列名 = 对应数据类型的值

short numShort =1;
int numInt = 1;
long numLong = 1;
float numFloat = 1.0f;
double numDouble = 1.0; //小数形式默认double 类型
char c = 'a'; //单引号括起来,必须包含一个字符,并且只能包含一个字符; char c = '' 会报语法错误
char c1 = '中';
bool b = true;
string str = "abcd"; //双引号括起来,可以包含0-n个字符;string str = "" 也是有效的赋值

数组的声明,初始化

int [] arr = { 1, 2, 3 }; //初始化方式一
int [] arr1 = new int[] { 1, 2, 3 }; //初始化方式二
int [] arr2 = new int[4]; //初始化方式三
arr2[0] = 1; //数组元素访问: 赋值
arr2[1] = 2;
arr2[2] = 3;
arr2[3] = 4;
Console.WriteLine("-------打印arr---------");
for (int i = 0; i < arr.Length; i++)
{
    Console.Write(arr[i] + "\t "); //数组元素访问: 调用
}
Console.WriteLine();
Console.WriteLine("-------打印arr1---------");
for (int i = 0; i < arr1.Length; i++)
{
    Console.Write(arr1[i] + "\t ");
}
Console.WriteLine();
Console.WriteLine("-------打印arr2---------");
for (int i = 0; i < arr2.Length; i++)
{
    Console.Write(arr2[i] + "\t ");
}

C#中 string 字符串对象:
关键字string 和 String 在C#中是完全一样的

实例化 :

string str1 = "Hello World"; //实例化方式一,也叫隐式的初始化(使用string)
String str2 = "Hello World"; //实例化方式一(使用String)

string str3 = new string(new char[]{'H','e','l','l','o',' ','W','o','r','l','d'}); //实例化方式二(使用string)
String str4 = new String(new char[]{'H','e','l','l','o',' ','W','o','r','l','d'}); //实例化方式二(使用string)

string 的常用方法

"Hello World" 对应的下标:

int IndexOf(string value) //返回指定字符在字符串中的第一匹配下标(左-->右);找不到匹配的字符返回-1

string str = "Hello World";
int index = str.IndexOf('l');
Console.WriteLine(index); //2

int LastIndexOf(String value) //返回指定字符在字符串中的第一匹配下标(右-->左);找不到匹配的字符返回-1

string str = "Hello World";
int index = str.LastIndexOf('l');
Console.WriteLine(index); //9 

string subString(int startIndex, int length) //从下标开始截取,包含下标,截取指定长度的字符

string str = "Hello World";
string strNew = str.Substring(2, 3);
Console.WriteLine(strNew); //llo

string ToUpper(string str); //将英文转换为大写

string str = "Hello World";
str = str.ToUpper();
Console.WriteLine(str); //HELLO WORLD

string ToLower(string str); //将英文转换为小写

string str = "Hello World";
str = str.ToLower();
Console.WriteLine(str); //hello world

string Trim(string str); //将字符串前后的空格删除

string str = "  Hello World  ";
Console.WriteLine("*" + str + "*"); //*  Hello World  *
str = str.Trim();
Console.WriteLine("*" + str + "*"); *Hello World*

string Join(string separator, string []value); //将字符串数字进行用指定字符拼接

string [] arr = { "H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d" };
string result = String.Join("+", arr);
Console.WriteLine(result);  //H+e+l+l+o+ +W+o+r+l+d

string []split(char sparator); //用指定字符将字符串类型转换为string数组

string str = "Hello World";
string[] arr = str.Split(" ");
Console.WriteLine("arr.Length " + arr.Length); //arr.Length 2
Console.WriteLine(arr[0]); //Hello
Console.WriteLine(arr[1]); //World

string string.format("格式字符串", 参数列表) //将参数列表中的值填充到格式字符串中对应的占位符上

string str = string.Format("姓名 {0}; 年龄 {1};", "王二狗",18); //调用string类的静态方法
Console.WriteLine(str); //姓名 王二狗; 年龄 18;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值