转义字符、强制转换与Convert转换

一、转义字符 

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);

        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值