当前位置:网站首页>给定区段范围内字符串自生成代码
给定区段范围内字符串自生成代码
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
边栏推荐
猜你喜欢

keytool: command not found

Authorization+Token+JWT

超级宝典&编程指南(红蓝宝书)-读书笔记

SAP TRANSLATE使用数据对象掩码示例

自定义时间格式(YYYY-MM-DD HH:mm:ss 星期X)

设置了body的最大宽度,但是为什么body的背景颜色还铺满整个页面?

如何判断点是否在多边形内(包含复杂多边形或者多边形数量很多的情况)

SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)

SAP pi / PO rfc2soap publishes RFC interface as WS example

King glory - unity learning journey
随机推荐
‘npm‘不是内部或外部命令,也不是可运行的程序 或批处理文件
4. Multi table query
防抖和节流
MySQL isolation level
14.事务处理
面经的总结
SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)
js之DOM事件
BTREE, B + tree and hash index
SAP PI / Po rfc2restful Publishing RFC interface as restful examples (proxy indirect)
Custom time format (yyyy-mm-dd HH: mm: SS week x)
Simple random roll call lottery (written under JS)
Authorization server (simple construction of authorization server)
数组扁平化
层次输出二叉树
状态同步与帧同步
移动布局(flex布局、视口标签)
手游性能优化
js之DOM学习三种创建元素的方式
页面动态显示时间(升级版)