如何检查字符串的第一个字符在vb中

时间:2013-01-08 01:43:30

标签: vb.net

我有以下代码,它从VB中的某些DateTimePickers读取日期和时间。

我需要能够确定第一个值是0还是1(例如09:12 ...或12:13 ......),如果它以0开头,则从中删除该字符字符串。

这是我到目前为止所拥有的,但无论如何都需要第一个角色。

    DateFrom = Form1.DateTimePickerFrom.Value.ToString
    DateTo = Form1.DateTimePickerTo.Value.ToString
    VarTimeFrom = Form1.HourTimePickerFrom.Value.ToString
    VarTimeTo = Form1.HourTimePickerTo.Value.ToString

    Dim DateFromManipulated = Left(DateFrom, 10)
    Dim DateToManipulated = Left(DateTo, 10)
    Dim TimeFromManipulated = Right(VarTimeFrom, 9)
    Dim TimeToManipulated = Right(VarTimeTo, 9)

    If Left(DateFromManipulated, 1) = 0 Then

        TimeFromMan = TimeFromManipulated.Remove(0, 1)
    Else
        TimeFromMan = TimeFromManipulated
    End If


    If Left(TimeFromManipulated, 1) = 0 Then

        TimeToMan = TimeToManipulated.Remove(0, 1)
    Else
        TimeToMan = TimeToManipulated
    End If

    Console.WriteLine(DateFromManipulated)
    Console.WriteLine(TimeToMan)
    Console.WriteLine(TimeFromManipulated)
    Console.WriteLine(TimeFromMan)
    Console.WriteLine(DateToManipulated)
    Console.WriteLine(TimeToManipulated)

我得到以下内容:

09/11/2012
1:36:00 
06:36:00 
6:36:00 
08/01/2013
11:36:00 

提前致谢!

麦克

1 个答案:

答案 0 :(得分:4)

VB.NET中的字符串不会等于整数。你可以引用字符零,但是:

If DateFromManipulated(0) = "0"c Then DateFromManipulated = DateFromManipulated.Substring(1)

... 然而,您应该只是按照您希望的方式格式化日期:

Dim dateFrom As String = DateTimePickerFrom.Value.ToString("M/dd/yyyy H:mm:ss")

......例如。 (M没有前导零,而不是MM;与H相同。)您可以在此处找到所有格式字符串:http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx