C#基础(2)——字符操作

来源:互联网 发布:淘宝运营需要学美工吗 编辑:程序博客网 时间:2024/06/05 11:23

转义符

针对控制台:
\n——换行
\t——列表
\”——双引号
\b——退格键,删除前面内容,两端没效果

针对windows操作系统
\r\n——换行

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ChuangzhiConsel{    class Program    {        static void Main(string[] args)        {            String str = "我爱北京天安门,\r\n天安门前太阳升!";            System.IO.File.WriteAllText(@"C:\Users\Administrator\Desktop\111.txt", str);            Console.WriteLine("写入成功!");            Console.ReadKey();        }    }}

这里写图片描述

路径除了用转义\,还可以加“@”,起到取消‘\’的转义作用。
“@”作用将字符串按照原格式输出。

算术运算符

int meanPrice = 5/3;结果为整数,取整
int number = 10;
double d = number;隐式类型转换,自动
int number = (int)d;显式类型转换,强制

int n1=10;
int n2=3;
double n=n1/n2;结果3

若有一个为double类型,则提升为double
double n=n1*1.0/n2;结果3

小数点留两位:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ChuangzhiConsel{    class Program    {        static void Main(string[] args)        {            int n1 = 10;            int n2 = 4;            double n3 = n1 *1.0/ n2;            Console.WriteLine("{0:0.00}",n3);            Console.ReadKey();        }    }}

这里写图片描述

加加减减一元运算符操作

n++,表达式中,运算是原值,运算后再加一

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ChuangzhiConsel{    class Program    {        static void Main(string[] args)        {            int n = 10;            int result = 10 + n++;            Console.WriteLine("n:{0},result:{1}",n,result);            Console.ReadKey();        }    }}

这里写图片描述

++n,先自身累加,再参与表达式运算

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ChuangzhiConsel{    class Program    {        static void Main(string[] args)        {            int n = 10;            int result = 10 + (++n);            Console.WriteLine("n:{0},result:{1}",n,result);            Console.ReadKey();        }    }}

这里写图片描述

》a = 5 ; result = a++ + ++a * 2 + –a + a++;

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ChuangzhiConsel{    class Program    {        static void Main(string[] args)        {            int a = 5;            int result = a++ + ++a * 2 + --a + a++;            Console.WriteLine("n:{0},result:{1}",a,result);            Console.ReadKey();        }    }}

这里写图片描述
先一元运算符再二元运算符,
result = 5+7*2+6+6=31;
a = 7;

Convert类型转换

类型不兼容,String到int或者double,

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ChuangzhiConsel{    class Program    {        static void Main(string[] args)        {            String str = "123.3";            double n = Convert.ToDouble(str);            Console.WriteLine("{0}",n);            Console.ReadKey();        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ChuangzhiConsel{    class Program    {        static void Main(string[] args)        {            String str = "123";            int n = Convert.ToInt32(str);            Console.WriteLine("{0}",n);            Console.ReadKey();        }    }}

比较ToInt32、int.Parse、int.TryParse的区别

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ChuangzhiConsel{    class Program    {        static void Main(string[] args)        {            //转换不成功就抛异常,内部上调用了int.Parse,使用反编译工具可以看出。            int number1 = Convert.ToInt32("123");            Console.WriteLine(number1);            //也会抛异常,但效率高            int number2 = int.Parse("123");//还有double.Parse  char.Parse            Console.WriteLine(number2);            //int.TryParse:字符串转int,如果失败,返回FALSE,number3=0,有助于系统的运行效率            int number3 = 100;//随便值            bool b = int.TryParse("123df", out number3);//在C#中,方法就是函数,b为方法的返回值,"123df"为参数            Console.WriteLine(b);            Console.WriteLine(number3);            Console.ReadKey();        }    }}

这里写图片描述

复合赋值运算符

+=
-=
*=
/=
%=

关系运算符

bool : >
bool : <
bool : >=
bool : <=
bool : ==
bool : !=

逻辑运算符

&&与
||或
!非
当输入“y”或者“n”时才退出:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ChuangzhiConsel{    class Program    {        static void Main(string[] args)        {            string input = "";            while (input != "y" && input != "n")            {                Console.WriteLine("请输入y或者n退出!");                input = Console.ReadLine();            }            Console.ReadKey();        }    }}
原创粉丝点击