我想在Javascript中使用以下内容
如果str.charAt(index)(在集合中){".",",","#","$",";",":"}
是的,我知道这一定很简单,但我似乎无法正确理解语法.我现在拥有的是什么
theChar = str.charAt(i);
if ((theChar === '.') || (theChar === ',') || (theChar === ... )) {
// do stuff
}
Run Code Online (Sandbox Code Playgroud)
这有效,但必须有更好的方法.
编辑:我做了这个,但不确定它是否良好:
var punc = {
".":true,
",":true,
";":true,
":":true
};
if (punc[str.charAt[index]) { ...
Run Code Online (Sandbox Code Playgroud)
使用这些字符定义一个数组,然后只需在数组中搜索.一种方法是:
var charsToSearch = [".", ",", "#", "$", ";", ":"];
var theChar = str.charAt(i); /* Wherever str and i comes from */
if (charsToSearch.indexOf(theChar) != -1) {
/* Code here, the char has been found. */
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6075 次 |
| 最近记录: |