一、转义字符
namespace Demo转义字符
{
internal class Program
{
static void Main(string[] args)
{
//\n转行
Console.WriteLine("今天想摸鱼\n不想上班");
//\"
Console.WriteLine("我今年\"20\"岁");
//\t tab
Console.WriteLine("啊实打实打算 啊手动阀第三方");
Console.WriteLine("是的是的\t实打实打算");
//不想 班让 恒安阿三 \b删除前面的字符改成空格
Console.WriteLine("\b不想上\b班让人\b恒安阿三");
//\r\n 写入磁盘的时候换行
Console.WriteLine("脑瘫玩意给老子趴\r\n");
string a = "我去你大爷的\r\n垃圾玩意";
string path = "D:\\code\\test\\C#code\\Demo转义字符\\14.txt";
File.WriteAllText(path, a);
Console.WriteLine("成功写入");
//@ \a \\---\ D:\code\test\C#code\Demo转义字符\14.txt
Console.WriteLine("D:\\code\\test\\C#code\\Demo转义字符\\14.txt");
Console.WriteLine(@"D:\code\test\C#code\Demo转义字符\14.txt");
//@保留原格式
Console.WriteLine(@"学了C#也找不到好公司
找到了也不是大公司");
}
}
}
二、强制转换
namespace Demo强制转换
{
internal class Program
{
static void Main(string[] args)
{
//强制转换parse
//parse转换有局限性
//string a = "1";
只能把string类型转换成相对应的类型
parse 要转换的类型.parse(要转换的值);
//double b = double.Parse(a);
//Console.WriteLine(b);
//char c = char.Parse(a);
//Console.WriteLine(c);
//输入 返回的是string类型 int
//string d = Console.ReadLine();
//Console.WriteLine(d);
//int a = int.Parse(Console.ReadLine());
//Console.WriteLine(a);
//简单类型的强制转换
//int a = 10;
//double b = 20.9;
不是四舍五入
//a = (int)b;
//Console.WriteLine(a);
//short g = (short)a;
//Console.WriteLine(g);
//此方法行不通
//string s = (string)a;
//Console.WriteLine(s);
//ToString
//把其他类型转换成string类型 变量.tostring();
//tostring所有的类型都在
//object F12查看源码
//int a = 110;
//string b = a.ToString();
//Console.WriteLine(b);
//char c = 'a';
//b = c.ToString();
//Console.WriteLine(b);
//double d = 12.23;
//b = d.ToString();
//Console.WriteLine(b);
//bool e = true;
//b = e.ToString();
//Console.WriteLine(b);
int a = 110;
string b = a+"";
Console.WriteLine(b);
char c = 'a';
b = c+"";
Console.WriteLine(b);
double d = 12.23;
b = d+"";
Console.WriteLine(b);
bool e = true;
b = e+"";
Console.WriteLine(b);
//把string类型转换成其他类型
/* int a = 10;
string b = "220";
a=int.Parse(b);
Console.WriteLine(a);
*/
}
}
}
三、Convert转换
namespace DemoCovert
{
internal class Program
{
static void Main(string[] args)
{
//Convect.To你要转换的类型(要转换的值);
//framework:没有进行格式化
//core:进行格式化
//int
//string a = "10";
//int b = Convert.ToInt32(a);
//Console.WriteLine(b);
//double
//string a = "5.52";
//double b = Convert.ToDouble(a);
//Console.WriteLine(b);
//遵循四舍五入 靠近那个数字就变成那个数字
//double a = 10.12;
//int b = Convert.ToInt32(a);
//Console.WriteLine(b);
//a = 10.51;
//b = Convert.ToInt32(a);
//Console.WriteLine(b);
//a = 10.5;
//b = Convert.ToInt32(a);
//Console.WriteLine(b);
int a = 19;
string d = Convert.ToString(a);
Console.WriteLine(d);
}
}
}