String Compare

rich5

New member
Joined
Sep 26, 2008
Messages
1
Programming Experience
1-3
Hey guys

I have two strings
String 1 contains decimal ASCII ranges e.g. 065-066
String 2 contains data to check. e.g. the, cat, sat, on, the mat

I need to check string two, any data that is not in the approved ASCII string I will remove.

I'm stuck on how to go about comparin the string?

Many Thanks
 
Create a loop that will tranverse each character in your string
In each iteration of the loop, grab the next char in the string to compare
Use the Asc(YourChar) function to get the charactes ascii value
Compare the ascii value to the number range acceptable to you in the ascii chart
If it is not in that range, modify it by changing, removing etc...

I've found that simply removing unwanted characters can lead into complications such as throwing off a fixed limited file imports, losing original values etc. You may want to consider replacing the character with something that will identify that you did make changes.
 
Tom's approach should work, but seems terribly inefficient. Even assuming you have a conditional to drop out of the check at the first non-valid character you have still executed a Chr() function and a compare for each valid character you did pass.

So a string like this:
VB.NET:
"ÜThis string is bad, it starts with a silly character."
would be fairly efficient in your application, as the first check would fail the string. However, a string like this:
VB.NET:
"I am a really good string. I am nice and long and all of my characters are normal oh wait what is this! ╤"
would be terrible is you performed 50 some-odd look ups just to find that all were wasted.

Looking for a cleaner way to perform this operation, I have used Regular Expressions in the past. Take this following example:

VB.NET:
Dim myASCIIValuesRange As String = "065-066"                                    'Your example of ASCII Char range input
Dim myASCIIValuesMin As Char = Chr(CInt(myASCIIValuesRange.Substring(0, 3)))    'Spilt of the "minimum" value
Dim myASCIIValuesMax As Char = Chr(CInt(myASCIIValuesRange.Substring(4, 3)))    'Split of the "maximum" value

Dim myRegex As New Regex("[^" & myASCIIValuesMin & "-" & myASCIIValuesMax & "]")    'Regular Expression to match any value NOT between your specified Minimum and Maximum ASCII codes.

Dim myCheckString As String = "This Is My Awesome String To Be Checked"         'String to check for valid content.
        
If myRegex.Matches(myCheckString).Count > 0 Then
  Console.WriteLine("String Would Be Rejected.")
Else
  Console.WriteLine("String Would Be Accepted.")
End If

Console.WriteLine("Done.")
Console.ReadKey()

So to explain the above a little.

First we take your string that contains an ASCII range "065-066" and convert that to the two characters that represent the min and max or the range.

The myRegex string is simply a regular expression that as the question "is anything in this string NOT what I want. (it reads: "[^A-B]" in this case)

I then setup my "checkString" which is the string you would be checking the content of for valid input.

Then we check if myRegex has any matches on the myCheckString variable. Any match means the string is contains invalid characters and should be rejected. You could obviously loop here for all the "checkStrings" to be checked. (Dont loop all the code, as converting the range to characters and declaring the regex over and over is what we are trying to avoid.)

There you have it. For applications like this, Regular Expressions are VERY, VERY powerful.

(Also, you will need to import System.Text.RegularExpressions)

Post back with more questions if necessary!
 
Back
Top