当前位置:网站首页>给定区段范围内字符串自生成代码
给定区段范围内字符串自生成代码
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
边栏推荐
猜你喜欢
随机推荐
10.更新操作
Nacos/sentinel网关限流和分组 (代码)
SAP PI/PO rfc2RESTful 發布rfc接口為RESTful示例(Proxy間接法)
斐波拉去动态规划
基于NLP的软件安全研究(二)
[Ted series] how to get along with inner critics?
Nacos / sentinel gateway current limiting and grouping (code)
FSM finite state machine
4.多表查询
Visualization Road (IX) detailed explanation of arrow class
MySQL index
2. Restricted query
5.SQL99标准:内连接和外连接
Authorization+Token+JWT
SAP 导出Excel文件打开显示:“xxx“的文件格式和扩展名不匹配。文件可能已损坏或不安全。除非您信任其来源,否则请勿打开。是否仍要打开它?
简易随机点名抽奖(js下编写)
ABAP 实现发布RESTful服务供外部调用示例
7. sub query
Preliminary configuration of OpenGL super Dictionary (freeglut, glew, gltools, GLUT)
Implementation of MySQL persistence