简体   繁体   English

识别每个单词的最后一个字母

[英]Identify last letter of each word

    String str = "Ada apa dengannya axr"
    int count = 0;           

I want to identify if the last letter of each word is "a/r/n/z", if it's is one of the mentioned letter then count++ . 我想确定每个单词的最后一个字母是否为“ a / r / n / z”,如果它是所提到的字母之一,则为count++ I don't understand regex or matches yet, so an explanation would be really helpful. 我尚不了解正则表达式或匹配项,因此进行解释将非常有帮助。 Thank you. 谢谢。

Regular Expression will find all occurrences in your input string of what is logically equivalent to the regex. 正则表达式将在输入字符串中查找在逻辑上等同于正则表达式的所有匹配项。

The comments to the question are saying to find all occurrences of "a" or "n" or "r" or "z" which are followed by the end of the word (\\b). 问题的注释是说要找到所有出现的“ a”或“ n”或“ r”或“ z”,其后是单词(\\ b)的结尾。

Another way to do it could be to use the split function in java. 另一种方法是在Java中使用split函数。

String[] words = str.split(" ");
for (String word : words) {
   // if the last letter is what you are looking for, count++; 
}

This is far less efficient, but it is much more clear what the computer is doing, which may help you learn more. 这样的效率要差得多,但是却可以清楚地知道计算机在做什么,这可能有助于您了解更多信息。

Good luck! 祝好运!

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

相关问题 除每个单词的最后一个字母外,逐字反转字符串 - Reverse a string word by word except the last letter of each word 如何在循环中显示单词,在每次迭代时删除最后一个字母 - How to display the Word in a Loop removing the last letter at each iteration 设计一个比较器来命令单词,使每个单词的最后一个字母是下一个单词的第一个字母? - Designing a comparator to order words so that the last letter of each word is the first letter of the next word? 如何计算每个单词的字母 - How to count letter of each word 尝试将句子拆分成单词,然后交换每个单词的第一个和最后一个字母 - Trying to get a sentence to be split into words then swap the first and last letter of each word 如何遍历字符串列表以将每个单词的最后一个字母更改为UpCase? - How do I iterate through a list of Strings to change the last letter of each word toUpperCase? 在Java上替换单词中的第一个,中间和最后一个字母 - Replacing first, middle and last letter in a word on Java 句子中的字母计数不计最后一个字 - Letter count in sentence not counting last word 如何使单词的最后一个字母变为大写 - How to make the last letter of a word become capitalized 检查单词的最后一个字母时,StringIndexOutOfBoundsException -1 - StringIndexOutOfBoundsException -1 when checking last letter of a word
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM