如何在vb 6.0中查找字符串上的字符串

时间:2013-03-09 17:06:37

标签: string search vb6

嗨我有这个问题在文本框中找到一个字符串 到目前为止,我所拥有它只检测逗号字符,现在我输入23 pm,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

如果有更好的方法可以通过更好地检测逗号和上午字符串来实现此目的。

2 个答案:

答案 0 :(得分:0)

使用instr()..

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

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

取自这篇文章..

VB6 Index of Substring

谢谢!

@leo

答案 1 :(得分:0)

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

使用InStr搜索上午或下午。

Replace上午和下午与&#34;&#34;并检查文本的其余部分是否有数字(用于验证)

' 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