简体   繁体   English

删除字符串中出现在特定字符之后的字符

[英]Remove characters in String that appear after specific character

In Google Apps Script, I'm attempting to remove all characters from a string that appear after a specific character. 在Google Apps脚本中,我试图从出现在特定字符之后的字符串中删除所有字符。

When I perform Logger.log(data) I get this: 当我执行Logger.log(data)时,我得到了:

[17-12-14 19:31:55:251 GMT] 
[17-12-14 19:31:55:252 GMT] ID
[17-12-14 19:31:55:253 GMT] 11111||q467jeX
[17-12-14 19:31:55:254 GMT] undefined
[17-12-14 19:31:55:255 GMT] 
[17-12-14 19:31:55:256 GMT] ID
[17-12-14 19:31:55:257 GMT] undefined
[17-12-14 19:31:55:258 GMT] 22222||K6OmenP
[17-12-14 19:31:55:259 GMT] 

I would like to remove all characters that come after "|", so that "data" looks like: 我想删除“ |”之后的所有字符,以便“数据”看起来像:

[17-12-14 19:31:55:251 GMT] 
[17-12-14 19:31:55:252 GMT] ID
[17-12-14 19:31:55:253 GMT] 11111
[17-12-14 19:31:55:254 GMT] undefined
[17-12-14 19:31:55:255 GMT] 
[17-12-14 19:31:55:256 GMT] ID
[17-12-14 19:31:55:257 GMT] undefined
[17-12-14 19:31:55:258 GMT] 22222
[17-12-14 19:31:55:259 GMT] 

I've tried using .split, .map and .subString methods, but I must be doing something wrong. 我尝试使用.split,.map和.subString方法,但是我一定做错了。 Does anyone know the best way to do this? 有谁知道最好的方法吗?

You can use JavaScript String split() Method 您可以使用JavaScript String split()方法

var text = "22222||K6OmenP";
var data = text.split("|")[0];
Logger.log(data);

.indexOf is the method you are looking for. .indexOf是您要查找的方法。 Do a x.substring(0,x.indexOf('|')); 做一个x.substring(0,x.indexOf('|'));

If this is a string in Javascript (the question is tagged javascript) you should be able to achieve your wanted result with a simple regex : 如果这是Javascript中的字符串(问题被标记为javascript),则应该可以使用简单的regex实现所需的结果:

const subject = `[17-12-14 19:31:55:251 GMT] 
[17-12-14 19:31:55:252 GMT] ID
[17-12-14 19:31:55:253 GMT] 11111||q467jeX
[17-12-14 19:31:55:254 GMT] undefined
[17-12-14 19:31:55:255 GMT] 
[17-12-14 19:31:55:256 GMT] ID
[17-12-14 19:31:55:257 GMT] undefined
[17-12-14 19:31:55:258 GMT] 22222||K6OmenP
[17-12-14 19:31:55:259 GMT]
`;

const result = subject.replace(/(\|.*)$/gm, '');

You must be doing console.log(str) or Logger.log(str) 您必须正在做console.log(str)Logger.log(str)

So try just replacing that str with String(str).subString(0, String(str).indexOf('|')) 因此,尝试仅用String(str).subString(0, String(str).indexOf('|'))替换该str

Thanks. 谢谢。

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

相关问题 在特定出现后删除字符串中的字符 - remove characters in string after specific occurrence 从右到左删除特定字符之后的所有字符 - Remove all characters after specific character from right to left 删除多行textarea中特定字符后的所有字符 - Remove all characters on line after a specific character in multiline textarea 在最后一次出现特定字符后,如何删除字符串的特定部分? - How to remove a specific section of a string after the last occurrence of a specific character? 删除第5个字符之后的字符串的所有字符? - remove all characters of a string after 5th character? 如何左右移动字符串的字符,每当出现特定字符时计数? - How to move left and right through characters of a string, counting whenever a specific character appear? 正则表达式-从第一个数字后的字符串中删除特定字符 - Regex - remove a specific character from a string except the first after number 从字符串中过滤掉特定字符。 如果它出现在某些字符的左侧,则只想将其删除 - Filter out a specific character from string. Only want to remove it if it appears to the left of certain characters 在特定字符jquery之后删除引号中的字符 - remove characters in quotes after a particular character jquery RegEx-在特定字符(#)之后获取所有字符 - RegEx - Get All Characters After A Specific Character (#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM