Comparing Alphanumeric strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • franzdawg3@hotmail.com

    #1

    Comparing Alphanumeric strings

    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.CompareO rdinal 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.CompareO rdinal
    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?=

    #2
    Re: Comparing Alphanumeric strings

    franzdawg3@hotm ail.com wrote:
    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.CompareO rdinal 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.CompareO rdinal
    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.

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • Shane

      #3
      Re: Comparing Alphanumeric strings

      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 ConvertNumInStr ing(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,
      "00000000000000 000000000000000 0")
      s = s.Remove(intSta rt, i - intStart) ' Clean off
      numeric chars
      s = s.Insert(intSta rt, strFormat) ' Numeric
      strings
      i = intStart + strFormat.Lengt h - 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

      Comment

      Working...