简体   繁体   English

比较从参数传递的字符串

[英]Comparing strings passed from arguments

Creating a node.js addon with C++. 用C ++创建一个node.js插件。 I want to check if the passed parameter is hi . 我想检查传递的参数是否为hi

The node app: 节点应用程序:

addon.Hello("hi");

The C++: C ++:

if (args[0]->ToString() != "hi") {
  ThrowException(Exception::TypeError(String::New("Sunday morning rain is falling...")));
  return scope.Close(Undefined());
}

But it keeps giving me the errors: 但是它总是给我错误:

../xxx.cc: In static member function 'static v8::Handle xxx::New(const v8::Arguments&)': ../xxx.cc:41:29: error: no match for 'operator!=' (operand types are 'v8::Local' and 'const char [6]') if (args[0]->ToString() != "hi") { ^ ../xxx.cc:在静态成员函数'static v8 :: Handle xxx :: New(const v8 :: Arguments&)':../xxx.cc:41:29:错误:'operator!不匹配!= '(操作数类型为'v8 :: Local'和'const char [6]'),如果(args [0]-> ToString()!=“ hi”){^

ToString() returns a String object. ToString()返回一个String对象。 You can get a C string from that with something like char* foo = *String::Utf8Value(args[0]->ToString()); 您可以从中获取C字符串,例如char* foo = *String::Utf8Value(args[0]->ToString()); .

Yeah I had this same problem, this seems to compile: 是的,我有同样的问题,似乎可以编译:

  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;

I basically arrived at my solution via the comments from the other answer, someone needed to just update a new answer with the actual solution which is what I have done here (I hope). 我基本上是通过其他答案的评论来找到我的解决方案的,有人需要用实际的解决方案更新一个新的答案,这就是我在这里所做的(我希望如此)。

#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")
{
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM