C#-----字符串反斜杠

C#中转义序列以斜杠(\)开头,当需要输出斜杠\时需要用双斜杠来表示它:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication1
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             string path = "F:\\MiloLu\\2015\\vs\\C#";
14             Console.WriteLine(path);
15             Console.ReadKey();
16         }
17     }
18 }

运行:

F:\MiloLu\2015\vs\C#

从上我们可以看出用双斜杠表示一个单斜杠,容易让人混淆,所以C#提供了另一种替代方式,在字符串前添加@

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace ConsoleApplication1
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             string path = @"F:\MiloLu\2015\vs\C#";
14             Console.WriteLine(path);
15             Console.ReadKey();
16         }
17     }
18 }

运行:

F:\MiloLu\2015\vs\C#

 

posted on 2019-10-07 15:50  Milo_lu  阅读(7963)  评论(0编辑  收藏  举报

导航