Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script Google Sheets应用程序脚本:在文本中查找字符位置&;子串_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script Google Sheets应用程序脚本:在文本中查找字符位置&;子串

Google apps script Google Sheets应用程序脚本:在文本中查找字符位置&;子串,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我试图在文本中找到字符(在本例中是冒号“:”)的位置,然后将文本子串到冒号之前。我使用string.substring(起始偏移量、结束偏移量)对文本进行子串。因此,基本上,我试图根据冒号在文本中的位置,使结束偏移量动态。谢谢你的帮助 string.substring(起始偏移量,cellA.find(“:”)选项1) slice和indexOf的组合应该适合您: var str = 'The quick brown fox jumps:over the lazy dog.'; Logger.l

我试图在文本中找到字符(在本例中是冒号“:”)的位置,然后将文本子串到冒号之前。我使用
string.substring(起始偏移量、结束偏移量)
对文本进行子串。因此,基本上,我试图根据冒号在文本中的位置,使
结束偏移量
动态。谢谢你的帮助

string.substring(起始偏移量,cellA.find(“:”)
选项1)
slice
indexOf
的组合应该适合您:

var str = 'The quick brown fox jumps:over the lazy dog.';
Logger.log(str.slice(0, str.indexOf(':')));
// expected output: "the lazy dog."
备选方案2) 使用
匹配
+常规表达式:

var str = 'The quick brown fox jumps:over the lazy dog.';
Logger.log(str.match(/^(.*)\:/)[1]);
// expected output: "the lazy dog."


感谢您的快速响应,Dmytro!我尝试了你的第一个解决方案(对我来说更容易理解),它成功了。