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. 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.
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.
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.
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
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 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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?
...
|
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...
|
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...
|
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.....");
|
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...
|
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
|
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
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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: 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...
|
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...
| |