读取字符串直到特定字符出现在 javascript 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12363516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read a string until a specific character appears in javascript
提问by Tsundoku
I am reading from a list of strings like this:
我正在从这样的字符串列表中读取:
April 9, 2012?(2012-04-09) num 0 April 16, 2012?(2012-04-16) num 0 April 23, 2012?(2012-04-23) num 0 April 30, 2012?(2012-04-30) num 0 May 7, 2012?(2012-05-07) num 0 May 14, 2012?(2012-05-14) num 0 October 8, 2012?(2012-10-08)[126] num 0 October 15, 2012?(2012-10-15)[126] num 0 October 22, 2012?(2012-10-22)[126] num 0 October 29, 2012?(2012-10-29)[126] num 0
2012 年 4 月 9 日?(2012-04-09) 编号 0 2012 年 4 月 16 日?(2012-04-16) 编号 0 2012 年 4 月 23 日?(2012-04-23) 编号 0 2012 年 4 月 30 日?(2012-16) 04-30) 编号 0 2012 年 5 月 7 日?(2012-05-07) 编号 0 2012 年 5 月 14 日?(2012-05-14) 编号 0 2012 年 10 月 8 日?(2012-10-08)[126] 编号0 2012 年 10 月 15 日?(2012-10-15)[126] 编号 2012 年 10 月 22 日?(2012-10-22)[126] 编号 0 2012 年 10 月 29 日?(2012-10-29)[126]编号 0
I want to read the date until the first parenthesis '(' I've tried:
我想读取日期直到第一个括号 '(' 我试过:
console.log( $(this).text().substring(0,16) );
but that only works with the longer months, the rest (like may or june) return part of the rest of the string. Is there a method I could use to do this?
但这仅适用于较长的月份,其余的(如五月或六月)返回字符串其余部分的一部分。有没有一种方法可以用来做到这一点?
回答by SnailCoil
You're looking for the indexof(str)
method
你在找indexof(str)
方法
myStr.substring(0, myStr.indexOf('('))
回答by web-nomad
Try this:
试试这个:
var a=$(this).text();
console.log( a.substr(0, a.indexOf('(')) ) );
回答by basiljames
Try console.log( $(this).text().substring(0, $(this).text().indexOf('(') );
尝试 console.log( $(this).text().substring(0, $(this).text().indexOf('(') );