1.
string s=@"c:\aa\b\1.jpg"; //c:\aa\b\1.jpg
string s="c:\\aa\\b\\1.jpg"; //c:\aa\b\1.jpg
string s="c:\aa\b\1.jpg"; //error,\作为转义字符,\a,\b可能有意义,但是\1 可能无意义,出错
2.
转义字符\只有在代码编译的时候起作用,对于用户的输入不起作用。也就是说:不会动态地对用户的输入字符串作转义解析。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace stringss
{
class Program
{
static void Main(string[] args)
{
string s1 = @"C:\a\b\1.jpg";
Console.WriteLine(s1); // C:\a\b\1.jpg
string s2 = "C:\\a\\b\\1.jpg";
Console.WriteLine(s2); // C:\a\b\1.jpg
//string s3 = "C:\a\b\1.jpg"; // error :\1 无法识别的转义序列
string s4 = Console.ReadLine(); //输入: a\nb\nc,不会对\n进行转义解析
Console.WriteLine(s4); //输出:a\nb\nc
string s5="a\nb\nc"; //s5会在编译的时候进行转义解析
Console.WriteLine(s5);
/*输出:
* a
* b
* c
*/
Console.ReadKey();
}
}
}