0

嗨,到目前为止,我在文本框中找到一个字符串时遇到了这个问题,这是我只检测到逗号字符,现在我输入 23pm,24,25am 我将如何使用此代码执行此操作,或者任何人都可以给我简单的代码?

Dim tdates() As String
Dim numberOfDates, xcount As Integer
tdates = Split(TXTDAYS.Text, ",")
numberOfDates = UBound(tdates)
Dim counter As Integer

' loop through each input
For counter = 0 To numberOfDates
    Dim xdate As String
    xdate = LCase$(tdates(counter))

If Len(xdate) <= 2 Then
  xcount = xcount + 1
  Else
        ' if the original text has am or pm in it, add .5
        If InStr(1, xdate, "am") > 0 Or InStr(1, xdate, "pm") > 0 Then
            xcount = xcount + 0.5 'problem here it doesn't count
        End If
    End If
Next

如果有更好的方法可以通过检测逗号和 am pm 字符串来更好地执行此操作。

4

2 回答 2

0

使用 instr()..

s = "admin@foo.com"
d = Mid(s, InStr(1, s, "@") + 1)

变量 d$ 将以字符串“foo.com”结尾。(不要忘记检查以确保存在 @ 符号,否则您最终会得到整个源字符串。)

取自这篇文章..

子字符串的VB6索引

谢谢!

@leo

于 2013-03-09T17:13:27.220 回答
0

Split逗号上的文字。然后你的数组将包含所有的单词

用于InStr搜索上午或下午。

ReplaceAM 和 PM 带有“”并检查文本的其余部分是否有数字(用于验证)

' split the input on a comma.
dim dates() as String = Split(TXTDAYS.Text, ",")
dim numberOfDates as Integer = UBound(dates)
dim counter as Integer

' loop through each input
For counter = 0 to numberOfDates
    dim dateEntered as String = LCase$(dates(counter))

    ' make sure the text entered is a number (once am and pm are removed)
    dim dateNumber as String =  Replace(Replace(dateEntered, "pm", ""), "am", "")
    if IsNumeric(dateNumber) Then
        COUNT = COUNT + 1

        ' if the original text has am or pm in it, add .5
        if Instr(1, dateEntered , "am") > 0 Or Instr(1, dateEntered , "pm") > 0 Then
            COUNT = COUNT + .5
        end if
    else
        ' do something to indicate invalid input
    end if 
Next
于 2013-03-09T20:01:45.617 回答