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(); } }}
阅读全文
0 0
- C#基础(2)——字符操作
- C#程序设计基础——转义字符
- Java基础操作——字符转换
- 字符界面基础操作
- 字符设备驱动基础篇4——字符设备驱动读写接口的操作实践
- C#基础-005字符介绍
- 黑马程序员——java基础——IO流(1)字符流与字节流基本操作
- Java基础——IO(一)——IO流概述、字节流、字符流、流操作规律
- C#基础(19)——path、File类的基本操作
- 黑马程序员——Java基础---IO流(字符流、字节流、转换流、流操作规律)
- 字符设备驱动基础篇5——驱动如何操控硬件(动静态映射操作LED)
- C# String操作(字符重复出现,截取字符,连接字符)
- C# IV: 数据库基础操作2
- C#文件操作基础(未完)
- C# Ftp基础操作
- C#基础操作,基础知识
- c#单链表基础操作
- C# Ftp基础操作
- 常见排序算法的复杂度以及稳定性
- 图片轮播的功能实现
- 选择排序的递归算法
- 解决CentOS 7,ATI显卡,屏幕亮度调节问题
- Java编程实现文件编码转换
- C#基础(2)——字符操作
- 今年最新的30个Android库
- BurpSuite安全测试
- ANDROID样式的开发:LAYER-LIST篇
- Java线程(一):synchronized
- 修改hosts不必重启 立刻生效
- Task Scheduler
- AlphaGo Zero的伟大与局限—ResNet作者、旷视研究院院长孙剑深度评述
- 一文了解机器学习开发工具