从标签上切下字符串 [英] Cut String from Label

查看:47
本文介绍了从标签上切下字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个包含字符串的标签(示例:日期)Label1.text = 2011/12/24

现在我要剪切Year(4个字符)并插入另一个Label(例如:label2 = 2011)

然后削减月份并插入label3(Label3 = 12)

最后将Day剪切并将其插入Label4(label4 = 24)

谢谢-

Hi
i Have a label include a String (Example : Date) Label1.text= 2011/12/24

Now i want cut Year (4 Character) and insert into another Label (Example : label2=2011)

Then Cut month and insert into label3(Label3 = 12)

And Finally Cut Day and insert it to Label4 (label4 = 24)

Thank you -

推荐答案

您有代码设计问题.不要剪任何东西.使用System.DateTime获取一些日期.假设所有标签的数据都应该相同.

现在—
You have a problem of code design. Don''t cut anything. Get some date using System.DateTime. Suppose this should be the same data for all labels.

Now—
synctor写道:
synctor wrote:

我有一个包含字符串的标签(例如:Label1.Text = "2011/12/24"). [拼写和代码固定-SA]"

I have a label include a string (example: Label1.Text = "2011/12/24"). [Spelling and code fixed — SA]"



啊哈别再吃了:-)

现在,使用相同的数据实例但格式不同,为不同的标签创建不同的字符串.使用自定义格式字符串使用System.DateTime.ToString(string customFormat): http://msdn.microsoft.com/zh-cn/library /8kb3ddd4.aspx [ ^ ].

将结果分配给不同标签的属性Text.就这样.

—SA



Aha. Stop having it. :-)

Now, create different strings for different labels using the same instance of data but different formats. Use System.DateTime.ToString(string customFormat) using custom format strings: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

Assign the results to the properties Text of different labels. That''s it.

—SA


最好的方法如下:

a best way to do is as follows

DateTime dt = Convert.ToDateTime(lblDate.Text);
lblYear.Text = dt.Year;
lblMonth.Text = dt.Month;
lblDate.Text = dt.Date;



或者也可以通过在字符串中查找来完成,但这不是一个好主意..我仍然在这里显示它..



or you can do by finding in string too, but its not a good idea.. still I am showing it here..

lblYear.Text = lblDateTime.Text.Substring(0,4);
lblMonth.Text = lblDateTime.Text.Substring(5,2);
lblDate.Text = lblDateTime.Text.Substring(8,2);



如果解决您的问题,则将其标记为答案,这会激发您的兴趣:)



mark as answer if solves your problem, it motivates :)


请看以下示例:
have a look at the following example :
Module Module1

    Sub Main()
    ' The file system path we need to split
    Dim s As String = "C:\Users\Sam\Documents\Perls\Main"

    ' Split the string on the backslash character
    Dim parts As String() = s.Split(New Char() {"\"c})

    ' Loop through result strings with For Each
    Dim part As String
    For Each part In parts
        Console.WriteLine(part)
    Next
    End Sub

End Module

Output

C:
Users
Sam
Documents
Perls
Main


这篇关于从标签上切下字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆