|
-
Jul 13th, 2003, 05:34 PM
#1
Thread Starter
Addicted Member
InStr and Mid crap [resolved]
ok i was told how to get just part of a line from a text file using InStr and Mid, and ive looked all over for how to do that, because i have no idea how to use InStr and Mid at all. i see things like
Code:
InStr(strCheck, Mid(sInvalidChars, i, 1)
but i have no idea why its setup like that, or what it means (ill post the entire script of the program dealy that had it inside). thats the closest thing to instructions on what InStr and Mid are that ive seen
Code:
Dim bCK As Boolean
Dim strDomainType As String
Dim strDomainName As String
Const sInvalidChars As String = "!#$%^&*()=+{}[]|\;:'/?>,< "
Dim i As Integer
bCK = Not InStr(1, strCheck, Chr(34)) > 0 'Check to see if there is a double quote
If Not bCK Then GoTo ExitFunction
bCK = Not InStr(1, strCheck, "..") > 0 'Check to see if there are consecutive dots
If Not bCK Then GoTo ExitFunction
' Check for invalid characters.
If Len(strCheck) > Len(sInvalidChars) Then
For i = 1 To Len(sInvalidChars)
If InStr(strCheck, Mid(sInvalidChars, i, 1)) > 0 Then
bCK = False
GoTo ExitFunction
End If
Next
Else
For i = 1 To Len(strCheck)
If InStr(sInvalidChars, Mid(strCheck, i, 1)) > 0 Then
bCK = False
GoTo ExitFunction
End If
Next
End If
If InStr(1, strCheck, "@") > 1 Then 'Check for an @ symbol
bCK = Len(Left(strCheck, InStr(1, strCheck, "@") - 1)) > 0
Else
bCK = False
End If
If Not bCK Then GoTo ExitFunction
strCheck = Right(strCheck, Len(strCheck) - InStr(1, strCheck, "@"))
bCK = Not InStr(1, strCheck, "@") > 0 'Check to see if there are too many @'s
If Not bCK Then GoTo ExitFunction
strDomainType = Right(strCheck, Len(strCheck) - InStr(1, strCheck, "."))
bCK = Len(strDomainType) > 0 And InStr(1, strCheck, ".") < Len(strCheck)
If Not bCK Then GoTo ExitFunction
strCheck = Left(strCheck, Len(strCheck) - Len(strDomainType) - 1)
Do Until InStr(1, strCheck, ".") <= 1
If Len(strCheck) >= InStr(1, strCheck, ".") Then
strCheck = Left(strCheck, Len(strCheck) - (InStr(1, strCheck, ".") - 1))
Else
bCK = False
GoTo ExitFunction
End If
Loop
If strCheck = "." Or Len(strCheck) = 0 Then bCK = False
ExitFunction:
ValidEmail = bCK
End Function
PS: i dont have any help files oO
go figure...
PS: thats not my code...
Last edited by R.a.B.B.i.T; Jul 15th, 2003 at 09:33 PM.
-
Jul 13th, 2003, 05:42 PM
#2
-
Jul 13th, 2003, 05:42 PM
#3
Hyperactive Member
Syntex of InStr is
(Start, Original String, String to be found)
This function will tell you where the String to be found is located in the Original String.
For example:
VB Code:
Msgbox InStr(1,"Hey You","You")
This code will show you 5, cuz the string "You" is starting in the 5th place of the original string.
-
Jul 13th, 2003, 05:45 PM
#4
Fanatic Member
This appears to be your problem:
VB Code:
Const sInvalidChars As String = "!#$%^&*()=+{}[]|\;:'/?>,< "
If you define your constant like this, the program will only do what you want when it encounters all those characters in that specific sequence. I'm not sure how to declare a multiple-setting constant, but I do know that a delimiter is used.
-
Jul 13th, 2003, 10:56 PM
#5
Thread Starter
Addicted Member
PS: i dont have any help files oO
go figure...
oooooo ok so thats how it works.
and now 1 more question:
how would i save "Hey" to a variable?
-
Jul 14th, 2003, 02:18 AM
#6
Fanatic Member
You could do something like this:
VB Code:
VariableName = Left$("Hey You", 3)
That will return the first three characters from the left end of the string. In this case, it would return "Hey."
Now if I did something like this:
VB Code:
VariableName = Right$("Hey You", 5)
That would return the first 5 characters from the right end of the string. This would return "y You." Mid$ has nothing to do with what you're trying to accomplish, although this would achieve the same thing as my Right$ example:
VB Code:
VariableName = Mid$("Hey You", 3)
It would skip to the 3rd position in the string, and return everything from that point. You would need to do a reverse Mid$ to use that function in this case, and I don't think there is a reverse Mid$.
I see you pretty much have InStr down pat though.
Get it now?
Last edited by hothead; Jul 14th, 2003 at 02:27 AM.
-
Jul 14th, 2003, 02:34 AM
#7
Fanatic Member
BTW Stiletto, you can also use InStr to check for specific characters. I do it all the time. Something like this would check for ('s and return a message box if one was found:
VB Code:
If InStr(1, strCheck, "(" Then
MsgBox "There is a ( in the string."
End If
-
Jul 14th, 2003, 12:18 PM
#8
Thread Starter
Addicted Member
nice....thx 4 the help, i finally understand WOOTTTTTTTT
-
Jul 14th, 2003, 03:19 PM
#9
Fanatic Member
It's not a problem. These functions are quite intimidating at first, but once you get the hang of them, you'll find they're actually quite easy.
-
Jul 15th, 2003, 08:23 PM
#10
Lively Member
Hothead, and all of you out there who think something "is easy". After all these year, I've finally come up with "THE ABSOLUTE TRUTH". It is:
BEFORE YOU KNOW HOW TO DO SOMETHING, "IT'S IMPOSSIBLE"; ONCE YOU LEARN HOW TO DO IT, "IT'S SO EASY"
-
Jul 15th, 2003, 08:27 PM
#11
Fanatic Member
That's my point. Re-read my post.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|