converts a char into a string  
Author Message
JaK82





PostPosted: Visual C# General, converts a char into a string Top

Hi All,

 

How would I convert this

string bleh = "hello world";

inputString = "r"

if (blehIdea == inputString)

{

blahIdea = inputString;

}

The error message I get is

Error 1 Operator '==' cannot be applied to operands of type 'char' and 'string' 

 

Much help appreciated

 



Visual C#9  
 
 
Matthew Watson





PostPosted: Visual C# General, converts a char into a string Top

Declare inputString as char, and do:

inputString = 'r'; // Note single quotes.

 
 
JaK82





PostPosted: Visual C# General, converts a char into a string Top

Sorry,

Need to explain a bit further,

The input string comes from

inputString = Console.ReadLine();

How would you convert this to a char,

Many Thanks


 
 
Ernst Kuschke





PostPosted: Visual C# General, converts a char into a string Top

You can call .ToCharArray() on a string instance to get an array of chars representing the string.


 
 
Mark Benningfield





PostPosted: Visual C# General, converts a char into a string Top

Hello All.

Jak82:

When you access a string with an index, what you get back is a type 'char', not 'string'. So, just do this:

if (bleh[j].ToString() == inputString)
{
.....
}

HTH.



 
 
ahmedilyas





PostPosted: Visual C# General, converts a char into a string Top

slight modification in Mark's example, you should be doing a .Equals() not a reference equals operator "==" :-)

 
 
Mark Benningfield





PostPosted: Visual C# General, converts a char into a string Top

Hello All.

ahmedilyas:

C# Programmer's Reference
== Operator (C# Reference)

For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings.

Okay, I'll bite. Am I missing something here



 
 
Matthew Watson





PostPosted: Visual C# General, converts a char into a string Top

[reply to ahmed]

That's not a reference equals, because strings are value types and the "==" operator is overloaded to do the right thing.

It's perfectly normal to compare strings using "==".


 
 
ShellShock





PostPosted: Visual C# General, converts a char into a string Top

Also when comparing strings, we always need to consider the case, so we could use

if (string.Compare(string1, string2, false, CultureInfo.InvariantCulture) == 0)

{

}


 
 
ahmedilyas





PostPosted: Visual C# General, converts a char into a string Top

I still wouldnt, I would still go for Equals() and its what I've been told and go by it always. I guess its personal preference but speaking to my colleagues...for string comparisons, Equals() would be the way.

Everyone has their own views/opinions/experiences, this is mine and so are a few of the Microsoft fellow friends/colleagues I have spoken to.



 
 
Mark Benningfield





PostPosted: Visual C# General, converts a char into a string Top

Hello All.

ahmedilyas:

I'm sorry you felt that it was necessary to delete my previous reply to your last post. I certainly did not mean to offend, and if I did, I apologize. I only meant to make the point (in what I thought -- although apparently mistakenly -- was a funny way) that in regard to strings, "==" is not a reference equals, as you stated earlier. And that your subsequent point that it is a matter of personal preference (while I agree completely), in light of the initial statement might perhaps be construed as a bit disingenuous. Again, I apologize if I offended, I certainly did not mean to.



 
 
Matthew Watson





PostPosted: Visual C# General, converts a char into a string Top

 

I still wouldnt, I would still go for Equals() and its what I've been told and go by it always. I guess its personal preference but speaking to my colleagues...for string comparisons, Equals() would be the way.

Everyone has their own views/opinions/experiences, this is mine and so are a few of the Microsoft fellow friends/colleagues I have spoken to.



Fair enough.

So, when doing string comparisons you do:

if (myString.Equals("XYZ"))
...

instead of:

if (myString == "XYZ")
...

Do you also do this:

if (myInteger.Equals(12345))
...

instead of:

if (myInteger == 12345)
...

If you don't, then: You should, according to your own
personal preference ("I would still go for Equals() and its what I've been told and go by it always").

However, in reality I think that you are mixing up reference with value semantics. If you stand back and think about it, you should realise that you should use "==" for things with value semantics, and ".Equals()" for things with reference semantics.

Otherwise, you end up having to use .Equals() instead of == for things like int, double, string, DateTime etc!

Having said that, one should use neither "==" nor "Equals()" for strings! Strings are different from other types, because they are case and region sensitive.

Therefore one should instead use one of the static String.Compare() methods so that one can control the region and case comparison.