计算字符串中的标点符号总数 - JavaScript
在英语中,下列字符全部被视为标点符号 −
'!', "," ,"\'" ,";" ,"\"", ".", "-" ,"?"
我们需要编写一个 JavaScript 函数,传入一个字符串并统计该字符串中出现这些标点符号的次数,然后返回该数量。
示例
下面是此函数的代码 −
const str = "This, is a-sentence;.Is this a sentence?";
const countPunctuation = str => {
const punct = "!,\;\.-?";
let count = 0;
for(let i = 0; i < str.length; i++){
if(!punct.includes(str[i])){
continue;
};
count++;
};
return count;
};
console.log(countPunctuation(str));输出
控制台中的输出: −
5
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP