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

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

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。 

猜你喜欢

转载自blog.csdn.net/johnsuna/article/details/120209001
今日推荐