2013-03-09 55 views
0

嗨我有这个问题找到一个文本框上的字符串 到目前为止,这是我只有它检测到逗号字符,现在ii输入23 pm,24,25am将如何我用这个代码来做这件事,或者任何人都可以给我简单的代码?如何在字符串中找到一个字符串在vb 6.0

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字符串来做到这一点更好。

+0

我修改这个代码我一直在寻找,这是我上来了。 – Denver 2013-03-09 19:20:35

+0

你能解释一下这个函数试图完成什么吗? – 2013-03-09 19:23:23

+0

即时通讯设法检测文本框中的am和pm与整个数字,整数相当于1和上午和下午是0.5,我认为这是错误的代码搜索整数是正确的,但是当我把am或pm的计算结果不正确,就像上面的例子23 pm,24,25pm 23 am = 0.5,24 = 1 and 25 pm = 0.5 – Denver 2013-03-09 19:27:49

回答

0

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

使用InStr来搜索am或pm。

Replace上午和下午有“”,选中该文本的其余部分为一个数字(验证)

' 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 
+0

我修改了代码,但其中一些id没有得到或者我无法转换为vb“dateNumber = tdate.Replace(”am“,”“).Replace(”pm“,”“)”= object required – Denver 2013-03-09 20:11:15

+0

sir where can我把我的新代码?我有修改代码似乎有一点点问题关于计数.5 – Denver 2013-03-09 20:28:11

+0

我不知道这是怎么回事xcount不会添加它自我xcount = xcount + .5返回0但如果xcount = xcount +1返回1 – Denver 2013-03-09 20:44:33

0

使用INSTR()..

s = "[email protected]" 
d = Mid(s, InStr(1, s, "@") + 1) 

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

这个职位采取..

VB6 Index of Substring

谢谢!

@leo

+0

如果我要检查上午和下午,我会将“@”更改为“上午”/“下午”。即时通讯使用循环来计算每一次检测“,”我也想同时检测上午和下午会是可能的吗? – Denver 2013-03-09 17:20:51

相关问题