VB. If statement?

Waffl3s

Well-Known Member
Joined
May 9, 2011
Messages
103
Reaction score
14
Hi Guys

Wondering how I would run a check on text to see if they have undesireable characters ie. " ?\ / ? etc.

New to programming. Please any help would be appreciated.
 
I don't program VB, but look through your API, there must be some kind of String contains function (str_pos(), etc).

Otherwise if you need something more complex look at a regex.
 
Just do a .Replace("?","") for whichever character you want to remove, or to check for a character use .Contains("?") or .IndexOf("?") to see if it exists in the string. Otherwise, if you only want alphanumeric characters, loop through your string as if it was an array and check for ascii chars that are not between char 48-57 (numbers) and 65-90 (upper case alpha) or 97-122 (lower case alpha). Google is your friend.
 
Something like
Code:
Public Shared Function IsAlphaNumeric(ByVal strToCheck As String) As Boolean
    Dim pattern As Regex = New Regex("[^a-zA-Z0-9]")

    Return Not pattern.IsMatch(strToCheck)
End Function
 
Something like
Code:
Public Shared Function IsAlphaNumeric(ByVal strToCheck As String) As Boolean
    Dim pattern As Regex = New Regex("[^a-zA-Z0-9]")

    Return Not pattern.IsMatch(strToCheck)
End Function

You'll need to add this to the top of your code:

Code:
Imports System.Text.RegularExpressions

Thought it was better for noob to avoid regex for now, though...
 
Short,sweet,i'm lazy when it comes to code when efficient ways exist to bypass issues :P

But ok,longer way avoiding RegEx

Code:
Public Function checkAlphaNumeric(strInputText As String) _
                                 As Boolean
Dim intCounter As Integer
Dim strCompare As String
Dim strInput As String
checkAlphaNumeric = False

For intCounter = 1 To Len(strInputText)
    strCompare = Mid$(strInputText, intCounter, 1)
    strInput = Mid$(strInputText, intCounter + 1, Len _ (strInputText))
    If strCompare Like ("[A-Z]") Or _
        strCompare Like ("[a-z]") Or _
        strCompare Like ("#") Then
        checkAlphaNumeric = True
    Else
        checkAlphaNumeric = False
        Exit Function
    End If
 Next intCounter

End Function
 
Thanks alot guys, really helped alot. Where did you guys learn all this varsity? youtube?
 
There is a certain logic you must grasp behind code,then after that you learn the basics of the language. Then you can either use pre-made solutions or craft your own

Remember,a calculation like 8x4 may seem complex for a 1st grader,however if you teach them it only means 8+8+8+8 it becomes much easier to grasp the basic concept
 
Thanks alot guys, really helped alot. Where did you guys learn all this varsity? youtube?

The basics from varsity, the real stuff that counts gets learned from experience. Just keep practising and you'll pick things up quickly enough. Stick to one language for now (VB) and once you are very comfortable with it you can branch out and learn something else quite easily. I've picked up a few scripting and programming languages very easily. Never VB though :)
 
The only way to really learn a language is to plug away at real world projects with it. It helps to start with a good manual or tutorial and find a good online resource for help - e.g. stackoverflow.com but the only way to really master any development environment is to spend a lot of time developing in it and then start seeking out more advanced patterns and methodologies. It helps to read and understand others code, if possible. Also, a good knowledge of how operating systems hang together, how memory is managed, how binary works, network transports, etc is essential for deeper insight.
 
Wow. VB... Its been a while. I was hoping we'd never meet again!
 
Top
Sign up to the MyBroadband newsletter
X