Comparing Strings

  • Thread starter Thread starter C# Learner
  • Start date Start date
C

C# Learner

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.
 
C# Learner said:
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.
 
Daniel said:
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.
 
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.
 
Chris R said:
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
 
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)



}
 
Shyam said:
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.
 

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

Back
Top