给定区段范围内字符串自生成代码

123 篇文章 2 订阅
119 篇文章 5 订阅

因项目原因,需要将一个区段范围内的字符串,自生成相关代码。比如:

string topLeft1ColorsString = "(3-16, 0)";
string topLeft2ColorsString = "(0,16-3)";
string topRight1ColorsString = "(17-30, 0)";
string topRight2ColorsString = "(33,3-16)";
string bottomRight1ColorsString = "(33,17-30)";
string bottomRight2ColorsString = "(30-17,33)";
string bottomLeft1ColorsString = "(16-3,33)";
string bottomLeft2ColorsString = "(0,30-17)";

如根据topLeft1ColorsString生成类似:(3,0),(4,0)....(16,0)的代码,扩展开来,就是生成下述代码:

Point[] topLeft1Points = new Point[] { new Point(3, 0), new Point(4, 0), new Point(5, 0), new Point(6, 0), new Point(7, 0), new Point(8, 0), new Point(9, 0), new Point(10, 0), new Point(11, 0), new Point(12, 0), new Point(13, 0), new Point(14, 0), new Point(15, 0), new Point(16, 0) };

同理:"(30-17,33)" 生成:(30,33),(29,33),(28,33).....(17,33)等等。 

如果字符串数量多,区间范围大,通过程序生成更加节省时间。

直接贴代码:

  public static string GetPointArray(string colorsString, string colorsName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Point[] " + colorsName + "= new Point[] {");
            List<string> leftStringList = new List<string>();
            List<string> rightStringList = new List<string>();
            int cc = 0;
            bool isMinusSignAtX = false; //减号是否在X坐标上
            bool isMinusSignAtY = false; //减号是否在Y坐标上
            string[] sArray = colorsString.Replace("(", "").Replace(")", "").Split(',');
            //得到3-16 0 或者: 0 16-3类似的数组
            if (sArray[0].IndexOf("-") >= 0) isMinusSignAtX = true;
            if (sArray[1].IndexOf("-") >= 0) isMinusSignAtY = true;
            if (isMinusSignAtX) //X坐标上含有"-"时
            {
                string[] ssArray = sArray[0].Split('-');  //得到3 16类似数组
                int number1 = int.Parse(ssArray[0]);
                int number2 = int.Parse(ssArray[1]);
                int diff = number2 - number1;
                int count = diff > 0 ? diff + 1 : diff - 1;
                cc = Math.Abs(count);
                List<string> tmpList = new List<string>();
                if (count > 0)
                {
                    //后者大于前者,升序
                    for (int j = 0; j < count; j++)
                    {
                        sb.Append("new Point(" + (number1 + j).ToString() + ",");
                        sb.Append(sArray[1] + ")");

                        if (j < count - 1) sb.Append(",");
                    }
                }
                else //前者大于后者,降序,类似16 - 3
                {
                    for (int j = 0; j < cc; j++)
                    {
                        sb.Append("new Point(" + (number1 - j).ToString() + ",");
                        sb.Append(sArray[1] + ")");

                        if (j < cc - 1) sb.Append(",");
                    }
                }
            }
            if (isMinusSignAtY) //Y坐标上含有"-"时
            {
                string[] ssArray = sArray[1].Split('-');  //得到3 16类似数组
                int number1 = int.Parse(ssArray[0]);
                int number2 = int.Parse(ssArray[1]);
                int diff = number2 - number1;
                int count = diff > 0 ? diff + 1 : diff - 1;
                cc = Math.Abs(count);
                List<string> tmpList = new List<string>();
                if (diff > 0)
                {
                    //后者大于前者,升序
                    for (int j = 0; j < cc; j++)
                    {
                        sb.Append("new Point(" + sArray[0] + "," + (number1 + j).ToString() + ")");

                        if (j < count - 1) sb.Append(",");
                    }
                }
                else //前者大于后者,降序,类似16 - 3
                {
                    for (int j = 0; j < cc; j++)
                    {
                        sb.Append("new Point(" + sArray[0] + "," + (number1 - j).ToString() + ")");

                        if (j < cc - 1) sb.Append(",");
                    }
                }
            }
            sb.Append("};");

            return sb.ToString();
        }

调用方式:

StringBuilder sb = new StringBuilder();
sb.AppendLine(GetPointArray(topLeft1ColorsString, "topLeft1Points"));
sb.AppendLine(GetPointArray(topLeft2ColorsString, "topLeft2Points"));
sb.AppendLine(GetPointArray(topRight1ColorsString, "topRight1Points"));
sb.AppendLine(GetPointArray(topRight2ColorsString, "topRight2Points"));
sb.AppendLine(GetPointArray(bottomRight1ColorsString, "bottomRight1Points"));
sb.AppendLine(GetPointArray(bottomRight2ColorsString, "bottomRight2Points"));
sb.AppendLine(GetPointArray(bottomLeft1ColorsString, "bottomLeft1Points"));
sb.AppendLine(GetPointArray(bottomLeft2ColorsString, "bottomLeft2Points"));

string res = sb.ToString();

OK。 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值