首页 > 其他分享 >给定区段范围内字符串自生成代码

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

时间:2023-02-27 21:04:56浏览次数:33  
标签:string Point int new 给定 sb 字符串 区段 Append


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

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。 

标签:string,Point,int,new,给定,sb,字符串,区段,Append
From: https://blog.51cto.com/JohnsonJu/6089163

相关文章

  • python用turtle画出给定图片的图像
    python用turtle画出给定图片的图像、校徽等复杂图像都可以需要:1.要画的图片2.安装好cv和turtle打开python文件,把想画的图片放到和py文件同目录,代码中默认图片名字为1.xxxxx......
  • 字符串根据逗号拆分转list
    Stringpath="123,456,789"if(path!=null&&path.indexOf(",")!=-1){String[]array=path.split(",");List<String>list=Arrays.asList(array);}/***逗号......
  • 【Python】判断字符串输入合法化
    Python判断字符串输入合法化​​只包含数字​​​​包含数字​​​​只包含中文​​​​包含中文​​​​只包含字母​​​​包含字母​​只包含数字判断字符串是否只包含数......
  • TypeScript String(字符串)
    TypeScriptString(字符串)String对象用于处理文本(字符串)。语法vartxt=newString("string");或者更简单方式:vartxt="string";String对象属性下表列出了Stri......
  • 文本左右对齐(字符串、模拟)、螺旋矩阵 II(数组、矩阵)、二叉树中的最大路径和(树、深
    文本左右对齐(字符串、模拟)给定一个单词数组和一个长度maxWidth,重新排版单词,使其成为每行恰好有maxWidth个字符,且左右两端对齐的文本。你应该使用“贪心算法”来放置......
  • 08. 字符串
    一、什么是字符串  字符串是一个或多个字符的序列,它使用一对双引号包裹起来,以'\0'作为字符串的结束标志。在C语言中,我们可以使用字符数组来保存字符串,也就是使用......
  • 力扣简557 反转字符串的单词2
    刚开始尝试像数组一样直接用找到一个单独的字符串就首尾双指针调换报错才知道java的String类一旦创建就不可以改变了又去看了字符串发现StringBuffer类型可以改变并且......
  • C语言填空:字符串中大写字母转小写,其他字条不变
    #include<stdio.h>main(){【1】ch[80];inti;printf("请输入一个字符串:");gets(【2】);for(i=0;ch[i]!='【3】';i++){......
  • 算法刷题 Day 56 | ● 583. 两个字符串的删除操作 ● 72. 编辑距离 ● 编辑距离总结
    583.两个字符串的删除操作本题和动态规划:115.不同的子序列相比,其实就是两个字符串都可以删除了,情况虽说复杂一些,但整体思路是不变的。https://programmercarl.com/0......
  • NOI 1.7编程基础之字符串
    11:潜伏者1.描述R国和S国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动。历经艰险后,潜伏于S国的R国间谍小C终于摸清了S国军用密码的编码规则:1、    S国军方内......