如何使用“+”运算符连接两个字符串?

jir*_*ec2 2 javascript string date google-earth-engine

在 Google Earth Engine 中,我需要从ee.Date对象生成文件名。我在 Google Earth Engine 中有以下代码:

var date_object = ee.Date.fromYMD(2017,12, 1);
var date_string = date_object.format("YYYY-MM-dd");
print(date_string);
file_name = "my_file_" + date_string;
print(file_name);
Run Code Online (Sandbox Code Playgroud)

print(date_string)看起来不错的输出:

2017-12-01
Run Code Online (Sandbox Code Playgroud)

但是 print(file_name) 的输出是:

    ee.String({
    "type": "Invocation",
    "arguments": {
        "date": {
          "type": "Invocation",
          "arguments": {
            "year": 2017,
            "month": 12,
            "day": 1
          },
          "functionName": "Date.fromYMD"
        },
        "format": "YYYY-MM-dd"
      },
      "functionName": "Date.format"
    })
Run Code Online (Sandbox Code Playgroud)

我希望我会得到输出my_file_2017-12-01。如何ee.String在 Google EarthEngine 中使用带有对象的“+”运算符来连接两个字符串?

Gen*_*yts 7

你看到的是一个代理。以下文档页面对此进行了解释:https : //developers.google.com/earth-engine/client_server。添加 getInfo() 修复了错误:

file_name = "my_file_" + date_string.getInfo();
Run Code Online (Sandbox Code Playgroud)

https://code.earthengine.google.com/e61868ab3f333e8f2d19afd96b396964

对于服务器端 EE 代码,正如 Nick 所建议的:

file_name = ee.String('my_file_').cat(date_string);
Run Code Online (Sandbox Code Playgroud)

https://code.earthengine.google.com/4812bb27a2869bd71771b067abd410e0

  • 或者,对于服务器端字符串:`ee.String("my_file_").cat(date_string)` (2认同)