当前位置:网站首页>给定区段范围内字符串自生成代码
给定区段范围内字符串自生成代码
2022-04-23 06:27:00 【大可山人】
因项目原因,需要将一个区段范围内的字符串,自生成相关代码。比如:
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。
版权声明
本文为[大可山人]所创,转载请带上原文链接,感谢
https://blog.csdn.net/johnsuna/article/details/120209001
边栏推荐
猜你喜欢
随机推荐
js之DOM事件
Configure NPM
AuthorizationServer(授权服务器的简单搭建)
MySQL storage engine
反思 | 事件总线的局限性,组件化开发流程中通信机制的设计与实现
VR、AR、MR的区别与应用,以及对AR技术的一些实现原理
SAP PI/PO登录使用及基本功能简介
instanceof的实现原理
【TED系列】如何与内心深处的批评家相处?
Learn to use search engines
MySQL index
2022.3.14 阿里笔试
反转链表练习
BTREE, B + tree and hash index
对js中argumens的简单理解
每日一题 | 曾被反转链表支配的恐惧
Redis connection error err auth < password > called without any password configured for the default user
简易随机点名抽奖(js下编写)
系统与软件安全研究(三)
自定义时间格式(YYYY-MM-DD HH:mm:ss 星期X)