- Home /
Comparing .NET Strings and JavaScript Strings
Question in short:
How do I compare a .NET String object with a JavaScript string or better yet: convert the one into the other?
Background:
I have a C# class handling tcp/ip socket connections. You can register functions with the class that handle incoming messages like so:
comm = new IPCommunicator("127.0.0.1", 9000); comm.addHandler(OnMessageReceived);
function OnMessageReceived(message) { // Handle message }
Now, the parameter passed to OnMessageReceived by the IPCommunicator class will be of a .NET String type. Inside the handler function I wish to be able to write code like:
if(message == 'some command') {
DoSomeCommand();
}
But of course .NET Strings and javascript strings can't be compared like that.
Answer by Mike 3 · Nov 18, 2010 at 01:09 PM
Javascript's String is the exact same one as string used in c# (Both are System.String)
Your snippet code was wrong is all, change it to this:
if(message == "some command") {
DoSomeCommand();
}
and it should work fine.
All of .NET's string functions are usable with Javascript
Your answer
Follow this Question
Related Questions
Ternary operator fails, but verbose comparison doesnt? 1 Answer
Java List. 0 Answers
Remove string value with substring 2 Answers
How can I compare directions of objects 2 Answers
Breaking up a string every A,B,C,D and store value behind it 3 Answers