java - How to create a boolean function that returns true if 's' is immediately followed by the letter 't' in a string -


this question has answer here:

i've started learn how write code using java , trying make boolean function takes string , returns true character 's' in string followed(somewhere) character 't', , false otherwise. example, "stay" should return true; "tta" should return false; "cc" should return true because 's' not appear @ therefore 's' preceded 't', there no 't's.how go this?

the easiest approach this

private static boolean findsfollowedbyt(string string) {     if (!string.contains("t") && !string.contains("s"))         return true;     if (string.contains("s") && string.indexof("s") < string.lastindexof("t"))         return true;     else         return false; } 

or more compact version

private static boolean findsfollowedbyt(string string) {     return !string.contains("t") && !string.contains("s") || string.contains("s") && string.indexof("s") < string.lastindexof("t"); } 

results test strings

"stay" returns: true "cc" returns: true "tta" returns: false "string" returns: true "tst" returns: true 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -