473,396 Members | 1,917 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,396 software developers and data experts.

Comparing Strings

I want to compare two strings, in a numeric fashion.

e.g.:

string a = "10";
string b = "2";

int comparison = String.Compare(a, b);
// comparison is now <0, but I want it to be >0 in this case
// since 10 is more than 2, numerically

Is this possible, without having to write my own comparison method?

I can't find an answer anywhere.
Nov 15 '05 #1
6 3110

"C# Learner" <cs****@learner.here> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I want to compare two strings, in a numeric fashion.

e.g.:

string a = "10";
string b = "2";

int comparison = String.Compare(a, b);
// comparison is now <0, but I want it to be >0 in this case
// since 10 is more than 2, numerically

Is this possible, without having to write my own comparison method?

I can't find an answer anywhere.


You will have to provide your own comparison. This is an issue of string
ordering, sometimes you want to order numbers numercially, othertimes as
strings. Most of the time you wish to order like a normal string and as such
that is the method provided. To sort numercially there are a few rules that
need to be handled that keeps it from being easy to implement as a
library(for example, should you count *all* numbers, or just the numbers at
the beginning\end? What about hex?).

The simplist method would be to convert the strings to integers and sort
those, but if your not able to, you'll have to write a method that does what
you need.
Nov 15 '05 #2
Daniel O'Connell [C# MVP] wrote:
You will have to provide your own comparison. This is an issue of string
ordering, sometimes you want to order numbers numercially, othertimes as
strings. Most of the time you wish to order like a normal string and as such
that is the method provided. To sort numercially there are a few rules that
need to be handled that keeps it from being easy to implement as a
library(for example, should you count *all* numbers, or just the numbers at
the beginning\end? What about hex?).

The simplist method would be to convert the strings to integers and sort
those, but if your not able to, you'll have to write a method that does what
you need.


Okay, thanks.
Nov 15 '05 #3
If you know that your strings will always represent some integer then:

if( a.Length > b.Length )
{
// a is greater
}
else if( a.Length < b.Length )
{
// b is greater
}
else
{
// use String.Compare(a, b);
// to determine which is greater
}

Chris R.

"C# Learner" <cs****@learner.here> wrote in message
news:#o**************@tk2msftngp13.phx.gbl...
I want to compare two strings, in a numeric fashion.

e.g.:

string a = "10";
string b = "2";

int comparison = String.Compare(a, b);
// comparison is now <0, but I want it to be >0 in this case
// since 10 is more than 2, numerically

Is this possible, without having to write my own comparison method?

I can't find an answer anywhere.

Nov 15 '05 #4
Chris R <so*****@hotmail.com> wrote:
If you know that your strings will always represent some integer then:


<snip>

That also has the restrictions of:

o No negative numbers
o No whitespace
o No leading zeros

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
Hey C#Learner,

Try using the following code

public class Tes

public static int StringCompare(string a,string b
{
int a1=0
int b1=0

tr

a1 = int.Parse(a)
b1 = int.Parse(b)

catch(FormatException ex
{ Console.WriteLine("Enter Numbers only");
catch(Exception ex
{ Console.WriteLine("Error" + ex.Message);

if(a1<b1
{ return -1;
else if(b1<a1
{ return 1;
els
{ return 0;
public static void Main(
{
string a ="a2"
string b ="14"

int comp=StringCompare(a,b);
Console.WriteLine(comp)

}
Nov 15 '05 #6
Shyam Remella wrote:
Hey C#Learner,

Try using the following code.


<snip>

Thankyou.

However, in this scenario, I'll have to hand-write the whole thing, I
believe, because the items I'm comparing are things like:

Test 1
Test 2
....
Test 10
Test 11
....
Test 20
....
Zzz 1
Zzz 2
....
Zzz 10

etc.
Nov 15 '05 #7

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...
26
by: William Park | last post by:
How do you compare 2 strings, and determine how much they are "close" to each other? Eg. aqwerty qwertyb are similar to each other, except for first/last char. But, how do I quantify that? ...
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...
6
by: BrianJones | last post by:
I have a problem with the int strcmp(str1,str2) function: When I do: char *pass; char *passv; pass = getpass("Please enter....."); passv = getpass("Please verify.....");
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...
2
by: Manny Chohan | last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny
15
by: luc.saffre | last post by:
Hello, here is something that surprises me. #coding: iso-8859-1 s1=u"Frau Müller machte große Augen" s2="Frau Müller machte große Augen" if s1 == s2: pass
1
by: Jetboy555 | last post by:
Sample input: 2000 Georgia Tech 30 Virginia 20 1999 Virginia 20 Virginia tech My Problem is in taking the input in correctly. I take the year in correctly, but i'm having trouble with the...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.