Java - Write code to get longest Common Prefix between two strings

Requirements

Write code to get longest Common Prefix between two strings

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String a = "book2s.com";
        String b = "book2s .com";
        System.out.println(longestCommonPrefix(a, b));
    }//from   w w  w.  j  a v a 2  s. c o m

    public static int longestCommonPrefix(String a, String b) {
        int i = 0;
        char[] as = a.toCharArray();
        char[] bs = b.toCharArray();
        int aLen = as.length;
        int bLen = bs.length;
        while (i < aLen && i < bLen && as[i] == bs[i])
            ++i;
        return i;
    }
}