如何分隔字符串中的每个字符并将每个字符放置在单独的标签中? [英] How can I separate each character in a string and place each character in a separate label?
问题描述
您好,我正在使用WinForms.
我的目标是拆分一个由18个字符组成的字符串,以便每个字符都被视为一个单独的值.
例如,如果232223232223323222是我的字符串,则需要将其拆分为18个单独的字符串:
2
3
2
2
2
3
2
....依此类推.
如何实现?
我尝试了几种方法:
Hello, I am using using WinForms.
My objective is to split a string of 18 characters so that each character is seen as a separate value.
For example, if 232223232223323222 is my string, I need to split it into 18 separate strings:
2
3
2
2
2
3
2
....and so on.
How can this be achieved?
I have tried several methods:
String a = 2223232223323222;
a.ToCharArray();
richTextBox1.Text = richTextBox1.Text + a;</pre>
和
and
string input = 2223232223323222;
double parts = 1;
int k = 0;
var output = input.ToLookup(c => Math.Floor(k++ / parts))
.Select(b => new String(b.ToArray()));
totalres.Text = output.ToString();
老实说,我什至不知道自己在做什么.
每个值都有自己的字符串(18个字符串)之后,如何将每个值放入自己的标签中?
我只要求您的解释中的详细程度为中高.
谢谢!
To be honest, i''m not even sure what I am doing.
After each value is its own string (18 strings) how can I place each one into its own label?
I only request that level of detail in your explanations is medium-high.
Thank you!
推荐答案
这是怎么回事:
What''s wrong with this:
int top = 0;
int gap = 4; //for example...
foreach (char ch in a) {
Label label = new Label();
label.Location = new Point(0, top);
label.Text = ch.ToString();
parent.Controls.Add(label);
top += gap + label.Height;
}
由于OP仍然在做……谁知道呢,我在位置计算中加入了细节.假设所有内容都放在适当大小的面板上,那么最左上角的位置是{0,0}.
As OP still does… who knows what, I added to detail with calculation of position. Let''s assume it all is put on some panel of appropriate size, so left-topmost position is {0, 0}.
你好
有一个Panel
和:
Hello
Have aPanel
and:
string a = "2223232223323222";
char[] chars = a.ToCharArray();
for (int i = 0; i < chars.Count(); i++)
{
Label l = new Label();
l.Text = chars[i].ToString();
this.MyPanel.Controls.Add(l);
l.Top = i * l.Height;
}
或者您也可以不使用Panel
:
or you can do it without Panel
:
this.Controls.Add(l);
如果我是你,我将创建一种方法来找出Label
s
的准确坐标
If I were you, I would create a method to figure out the accurate coordinate for Label
s
private void Form1_Load(object sender, EventArgs e)
{
string a = "2223232223323222";
char[] chars = a.ToCharArray();
for (int i = 0; i < chars.Count(); i++)
{
Label l = new Label();
l.Text = chars[i].ToString();
this.MyPanel.Controls.Add(l);
SetCoordinate(l, i);
}
}
private void SetCoordinate(Label lable, int i)
{
// some code to computing
}
但是:
如果Form
中有许多Label
,请标记它们.
将其标签设置为0、1、2、3和...
然后:
But:
If You have many Label
s in your Form
, then tag them.
Set their Tags to 0, 1, 2, 3 and ...
Then:
private void Form1_Load(object sender, EventArgs e)
{
string a = "2223232223323222";
char[] chars = a.ToCharArray();
for (int i = 0; i < chars.Count(); i++)
{
Label l = FindLabel(i);
if (l == null)
continue;
l.Text = chars[i].ToString();
}
}
private Label FindLabel(int i)
{
foreach (Control c in this.Controls)
{
if (c.Tag == null)
continue;
int k = 0;
if (!int.TryParse((c.Tag).ToString(), out k))
continue;
if (c is Label && k == i)
return (Label)c;
}
return null;
}
这篇关于如何分隔字符串中的每个字符并将每个字符放置在单独的标签中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!