GAS - 从字符串中拆分新行中的字符串会添加逗号

问题描述 投票:0回答:1

我试图在删除新行输入后在两个字符串之间创建比较。但是,当对字符串使用 split 方法时,由于字符串中添加了逗号(,),因此比较失败。无法弄清楚为什么会出现这种情况。感谢您的帮助

      var test_Array = ['Chess', 'Chess\n'];
      
      if (test_Array[0] == test_Array[1].split('\n'))
      {
        console.log('Test');
      }

      console.log(test_Array[0] + ' - ' + test_Array[1].split('\n'));

arrays string google-apps-script split
1个回答
0
投票

尝试这样:

function myfunk() {
  var test_Array = ['Chess', 'Chess\n'];
  if (test_Array[0] == (test_Array[1].split('\n')[0])) {
    Logger.log('Test');
  }
  
  Logger.log(test_Array[0] + ' - ' + test_Array[1].split('\n')[0])
}

逗号只是分割的默认分隔符

Execution log
9:55:06 AM  Notice  Execution started
9:55:07 AM  Info    Test
9:55:07 AM  Info    Chess - Chess
9:55:08 AM  Notice  Execution completed
© www.soinside.com 2019 - 2024. All rights reserved.