简体   繁体   中英

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++ . 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).

Another way to do it could be to use the split function in java.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM