首页 > 代码库 > asp.net,copy网页的代码去掉格式化代码前面的序号

asp.net,copy网页的代码去掉格式化代码前面的序号

有时候会遇到这种情况,从网页上copy的代码(代码是格式化状态的)会保留格式化前面的序号,比如下面:

public interface IPerson
 2{
 3    string FirstName { get; set; }
 4    string LastName { get; set; }
 5    DateTime BirthDate { get; set; }
 6}
 7
 8public class Employee : IPerson
 9{
10    public string FirstName { get; set; }
11    public string LastName { get; set; }
12    public DateTime BirthDate { get; set; }
13
14    public string Department { get; set; }
15    public string JobTitle { get; set; }
16}
17
18public class PersonConverter : CustomCreationConverter<IPerson>
19{
20    public override IPerson Create(Type objectType)
21    {
22        return new Employee();
23    }
24}

这真是非常的不爽啊,得手动一个个的去掉,shit。(经过同事询问,才知道,vs中按住Alt键选择,然后去掉即可,真是后知后觉,现在才知道),下面我们考虑通过代码的方式一次性处理掉:

讲代码copy到一个txt文本,放到工程的copycode文件夹下,然后用下面的代码

public string GetCode()
        {
            string path = Server.MapPath("copycode/code.txt");
            string[] allCodeLine = System.IO.File.ReadAllLines(path, System.Text.Encoding.UTF8);

            System.Text.StringBuilder strb = new System.Text.StringBuilder();
            Enumerable.Range(0, allCodeLine.Count()).ToList().ForEach(x =>
            {
                string output = System.Text.RegularExpressions.Regex.Replace(allCodeLine[x].ToString().Trim(), "^(\\d*)", "");
                strb.AppendLine(output);
            });
            return strb.ToString();
        }

搞定,然后copy到vs中,ctrl+k+d快捷键格式化处理就ok了:

public interface IPerson
    {
        string FirstName
        {
            get;
            set;
        }
        string LastName
        {
            get;
            set;
        }
        DateTime BirthDate
        {
            get;
            set;
        }
    }
    public class Employee : IPerson
    {
        public string FirstName
        {
            get;
            set;
        }
        public string LastName
        {
            get;
            set;
        }
        public DateTime BirthDate
        {
            get;
            set;
        }
        public string Department
        {
            get;
            set;
        }
        public string JobTitle
        {
            get;
            set;
        }
    }
    public class PersonConverter : CustomCreationConverter
    {
        public override IPerson Create(Type objectType)
        {
            return new Employee();
        }
    }
可以做成一个小工具,方便我们学习,coding,测试