Find common sequence (prefix) of characters in multiple strings in Java

This example shows how to find a common sequence of characters at the beginning of any number of Strings. The Apache Commons Lang library comes with a method called getCommonPrefix() to which you pass an array of Strings to be compared. The method returns the initial sequence of characters that is common to all of them. If no common sequence can be found, including if all values are null in the array, an empty String will be returned:

and here’s the output:

Search for more Java info on this site here:
Custom Search

Please type any questions here.