简体   繁体   English

如何检查字符串中的某个字符是否在另一个字符之后?

[英]How to check whether a certain character in string comes after another character?

Given a string aaabbbcccooeellmmzzz how can I count the number of characters that comes after 'm'.给定一个字符串aaabbbcccooeellmmzzz ,我如何计算 'm' 之后的字符数。 For example in this case it should return 5 (ie oozzz);例如在这种情况下它应该返回 5(即 oozzz);

Note: Here o and z characters come after 'm' in alphabetical order not about their index position after m.注意:这里 o 和 z 字符按字母顺序出现在“m”之后,而不是它们的索引 position 在 m 之后。

So i want to count the characters (n,o,p,q,r,s,t,u,v,w,x,y,z) as many times they appear in the string.所以我想计算字符(n,o,p,q,r,s,t,u,v,w,x,y,z)在字符串中出现的次数。

Sample Input: aaabbbcccooeellmmmzzz样本输入: aaabbbcccooeellmmmzzz
Sample Output: 5样品 Output: 5

Use substring to remove everything from the start to the index of the last occurrence of the character, then get the length:使用substring删除从开始到字符最后一次出现的索引的所有内容,然后获取长度:

 let character = 'm'; let str = 'aaabbbcccooeellmmmzzz'; let res = str.substring(str.lastIndexOf(character) + 1).length; console.log(res)

暂无
暂无

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

相关问题 Javascript正则表达式来检查一个字符串是否是某个字符的组合 - Javascript regular expression to check whether a string is a combination of certain character 如何检查字符串中的字符是否是 JavaScript 中的特定字符? - How do I check whether a character in a string is a specific character in JavaScript? 在一个字符串中,如何检查另一个字符前面的前一个字符是否是空格? - Within a string, how to check whether the previous character ahead of another one is a space? 在特定字符之后,如何匹配字符串中的第一个字符? - How do I match the first character in string, AFTER a certain character? 检查字符串是否以某个字符开头 - Check if a string is preceded by a certain character 如何检查特定字符串中是否存在任何特殊字符 - how to check whether any special character is present in a particular string JavaScript:如何获取紧接在另一个特定字符之后的某个特定字符的出现索引? - JavaScript: How to get index of the occurrence of a certain character that is immediately after another certain character? 如何检查一个字符串是否包含另一个字符串中的任何字符? - How to check if a string contains any character from another string? Javascript 检查字符串是否仅包含某个字符 - Javascript check if string contains only certain character 如何使用javascript或jquery隐藏某个字符后的字符串结尾 - How to hide the end of a string after a certain character, with javascript or jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM