if(word_a != word_b) then ...

  • Thread starter Thread starter Adrian
  • Start date Start date
A

Adrian

if(word_a != word_b) then .... this (i.e., comparing
strings) doesn't seem to be allowed in C# - or is there
a way?

Please respond,
thank you,
Adrian.
 
I'm not sure if I understood your question. But this works for me:

string s1 = "Hello";
string s2 = "Hello";
Console.WriteLine("Are strings different? {0}",
(s1 != s2) ? "Yes!" : "No!");

kind regards,

matthias
 
Hi,

It does work of course,

Post an example with your problem and with the EXACT contents of the
strings

maybe you have them with different cases, do this:

if( word_a.ToLower() != word_b.ToLower() )


cheers,
 
Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

It does work of course,

Post an example with your problem and with the EXACT contents of the
strings

private void Form1_Load(object sender, System.EventArgs e)

{

if("monkey" < "donkey")

panel1.Text = "Monkey is smaller than donkey";

else

panel1.Text = "Monkey is not smaller than donkey";

}
 
Equality and inequality work. You're probably thinking of the
operators < and > (and <= and >=). These don't work on
strings in C# - you need to use the CompareTo method.

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
You can't use "<" and ">" on string, but you asked if you could use "!=";
that is possible:

if("monkey" != "donkey")
panel1.Text = "Monkey is not donkey";
else
panel1.Text = "Monkey is donkey";

If you want to be able to say monkey < donkey you could use an enum like
this:

public enum Animals
{
Monkey = 0,
Donkey = 1
}

if(Animals.Monkey<Animals.Donkey)
{
//do something
}
else
{
//do something else
}
 
Adrian said:
private void Form1_Load(object sender, System.EventArgs e)

{

if("monkey" < "donkey")

panel1.Text = "Monkey is smaller than donkey";

else

panel1.Text = "Monkey is not smaller than donkey";

}

That's an entirely different matter. != is defined for strings, but <
isn't. Use String.Compare or String.CompareOrdinal to compare two
strings. (Or String.CompareTo...)
 
spmm# said:
You can't use "<" and ">" on string, but you asked if you could use "!=";
that is possible:

if("monkey" != "donkey")
panel1.Text = "Monkey is not donkey";
else
panel1.Text = "Monkey is donkey";

If you want to be able to say monkey < donkey you could use an enum like
this:

public enum Animals
{
Monkey = 0,
Donkey = 1
}

if(Animals.Monkey<Animals.Donkey)
{
//do something
}
else
{
//do something else
}
 
spmm# said:
You can't use "<" and ">" on string, but you asked if you could use "!=";
that is possible:
Apologies for the messy question.
Working until late last night.

My problem is that I need to sort more than 100,000
words. So I thought I'd do it in batches. Have a batch
in alphabetical order, then sort in another batch, etc,
etc. But not being able to do < and >, I cannot sort
in the new batch into the existing one. So I am kind of
stuck.
 
If you want to sort your strings you should put them in an array. Then it's
easy to sort them:

string[] arr = {"asd","wer", "bddfb", "yuioty", "ksadfa"};
Array.Sort(arr);
foreach(string s in arr)
{
//write to console, or do whatever:
Console.WriteLine(s);
}

this gives the following output:
asd
bddfb
ksadfa
wer
yuioty

I'm not sure if this way of doing it will give performance problems on
100.000 records... try it.
 
Hi,

It's a different thing < than !=

If you want to compare 2 strings you can use String.Compare or
String.CompareTo

Cheers,
 
in message <snipped>

Thank you all for the responses. I will know what to do in future.
In the mean time I had to get on with the job and quickly wrote
a small BASIC program that did the job.
 
Back
Top