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.jav a2s  .  c om

    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;
    }
}