473,387 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.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?

May 4 '07 #1
2 4706
fr********@hotmail.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.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.

--
Göran Andersson
_____
http://www.guffa.com
May 4 '07 #2
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

May 4 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: beliavsky | last post by:
By mistake I coded something like print ("1" > 1) and got the result "True". Comparing an integer and a string seems meaningless to me, and I would prefer to have an exception thrown. Can...
5
by: Curtis Gilchrist | last post by:
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
10
by: Bob | last post by:
Sorting the following alphanumerics using myArray.sort(): 04-273-0001 04-272-0001 04-272-0003 04-272-0001 04-273-0001 Results in:
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
0
by: richardkreidl | last post by:
I have the following hash script that I use to compare two text files. 'Class Public Class FileComparison Public Class FileComparisonException Public Enum ExceptionType U 'Unknown A 'Add...
8
by: dohyohdohyoh | last post by:
I have a programming question to generate an ordered list of alphanumeric strings of length 4. two alphabets rest numberst, etc. EG 0000-9999 then A000-Z999 then AA00 to ZZ99 then AAA0 - ZZZ9...
3
by: dohyohdohyoh | last post by:
I have a programming question to generate an ordered list of alphanumeric strings of length 4. two alphabets rest numberst, etc. EG 0000-9999 then A000-Z999 then AA00 to ZZ99 then AAA0 - ZZZ9...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.