比较从参数传递的字符串

时间:2014-06-16 13:22:04

标签: c++ node.js v8

使用C ++创建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:错误:没有   匹配'operator!='(操作数类型是'v8 :: Local'和   'const char [6]')        if(args [0] - > ToString()!=" hi"){                                ^

3 个答案:

答案 0 :(得分:1)

ToString()返回一个String对象。您可以使用类似char* foo = *String::Utf8Value(args[0]->ToString());的内容获取C字符串。

答案 1 :(得分:0)

是的,我有同样的问题,这似乎是编译:

  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;

我基本上通过其他答案的评论来达到我的解决方案,有人需要用实际解决方案更新新答案,这是我在这里所做的(我希望)。

答案 2 :(得分:0)

#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")
{
    ...
}
相关问题