The opposite of finding the common suffix this example will find the longest common prefix between two words using java, guava and apache commons.
Straight up Java
This snippet will find the common prefix of two strings using java. We will take the minimum length of both Strings as the prefix in question won't be greater. Next using a for loop we will iterate and compare each element at the specific position. If the char doesn't match we will break the loop.
Guava's Strings.commonPrefix will find the longest string prefix of two words.
Apache commons StringUtils.getCommonPrefix will find the initial sequence of characters that is common in an array of strings.