比较从parameter passing的string
用C ++创build一个node.js插件。 我想检查传递的参数是否hi
。
节点应用程序:
addon.Hello("hi");
C ++:
if (args[0]->ToString() != "hi") { ThrowException(Exception::TypeError(String::New("Sunday morning rain is falling..."))); return scope.Close(Undefined()); }
但它一直给我的错误:
../xxx.cc:在静态成员函数'static v8 :: Handle xxx :: New(const v8 :: Arguments&)':../xxx.cc:41:29:error:'operator!= no' '(操作数types是'v8 :: Local'和'const char [6]')if(args [0] – > ToString()!=“hi”){^
ToString()
返回一个String对象。 你可以从类似char* foo = *String::Utf8Value(args[0]->ToString());
得到一个Cstringchar* foo = *String::Utf8Value(args[0]->ToString());
。
是的,我有这个问题,这似乎编译:
using namespace std; v8::String::Utf8Value arg2(args[2]->ToString()); if (strcmp(*arg2, "true") != 0 && strcmp(*arg2, "false") != 0) { Nan::ThrowTypeError("Third argument to 'replace-line' must be a string representing whether to find or replace."); return; } bool isReplace = strcmp(*arg2, "false") == 0 ? false : true;
我基本上是通过其他答案的评论来达到我的解决scheme的,有人需要用实际的解决scheme更新一个新的答案,这是我在这里所做的(我希望)。
#include <string> #include <node.h> String::Utf8Value tmp(args[0]->ToString()); arg0 = std::string(*tmp); //then you can compare string using "== or > or <" if(arg0 == "hi") { ... }