Comparing Alphanumeric strings

F

franzdawg3

I find it hard to believe that there is not a native solution to this
problem built into VB.NET but based on what I've come up with from
MSDN and google if there is one it is not obvious.

I have 2 situations that I need to handle through string comparison.
NOTE: I am interested in comparison NOT SORTING although they seem
hand in hand.

The first situation is already handled and it is that if I have 3
strings "d1", "d2", and "d11", using String.Compare, or
String.CompareOrdinal works fine as it would arrange these as "d1",
"d11", "d2".

My problem occurs with the 2nd situation which with the same 3 strings
should be "d1", "d2", "d11". String.Compare and String.CompareOrdinal
both think that the string "d2" is greater than the string "d11". Is
there anything built into VB.NET that performs right justified
comparisons?
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

I find it hard to believe that there is not a native solution to this
problem built into VB.NET but based on what I've come up with from
MSDN and google if there is one it is not obvious.

I have 2 situations that I need to handle through string comparison.
NOTE: I am interested in comparison NOT SORTING although they seem
hand in hand.

The first situation is already handled and it is that if I have 3
strings "d1", "d2", and "d11", using String.Compare, or
String.CompareOrdinal works fine as it would arrange these as "d1",
"d11", "d2".

My problem occurs with the 2nd situation which with the same 3 strings
should be "d1", "d2", "d11". String.Compare and String.CompareOrdinal
both think that the string "d2" is greater than the string "d11". Is
there anything built into VB.NET that performs right justified
comparisons?

If there was anything like a right justified comparison, it would not
help you. It would arrange the strings "d11", "d1", "d2".

You have to parse the strings so that you can compare the numeric part
of the strings as numbers, not strings.
 
S

Shane

I got around this problem by creating a new IComparer class to do my
sorting.

I call this function before doing comparisons. The function finds a
series of
contiguous digits, and replaces the section of string with a zero
filled string.
I arbitrarily set the string to 30 places, since I needed to pick a
finite number.
The function will search for multilple instances of contiguous digits.

Private Function ConvertNumInString(ByVal s As String) As String
Dim i As Integer ' Current position
Dim intDigit As Integer ' Each digit
Dim intLen As Integer ' Length of the string
Dim intStart As Integer ' Start of a numeric
string
Dim decTot As Decimal ' Total of contiguous
chars
Dim strFormat As String ' Hold the formatted
string

intLen = s.Length ' Number of characters
intStart = -1 ' Not working on a
digit string
Do While i < intLen ' Analyze the string
intDigit = Asc(s.Substring(i, 1)) - 48 ' Get the current
digit
If intDigit >= 0 And intDigit <= 9 Then
decTot = decTot * 10 + intDigit ' Calculate digit
If intStart = -1 Then intStart = i ' Flag as started
Else
If intStart <> -1 Then ' Process the digits
with leading zeros
strFormat = Format(decTot,
"000000000000000000000000000000")
s = s.Remove(intStart, i - intStart) ' Clean off
numeric chars
s = s.Insert(intStart, strFormat) ' Numeric
strings
i = intStart + strFormat.Length - 1
intLen = s.Length ' New Number of
characters
decTot = 0 ' Reset total for next
string
intStart = -1 ' Reset numeric string
End If
End If
i += 1 ' Next character
Loop
Return (s) ' Updated string
End Function

Shane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top