Return a String containing only alphabets without spaces Scenario: Return a String that contains only alphabets without spaces.

input = "A man, a plan, a canal: Panama"

output = "AmanaplanacanalPanama"

Can you please critique my code?

private String OnlyAlphabets(final String string) {
    StringBuilder sb = new StringBuilder();
    char ch;

    for (int i = 0; i &lt; string.length(); i++) {
        ch = string.charAt(i);
        if (!Character.isAlphabetic(ch)) {
            continue;
        }
        sb.append(ch);
    }
    return sb.toString();
}
char ch;



In general, we want to declare as late and at as small a scope as possible.  You never use this outside the loop, so it could just be 

        char ch = string.charAt(i);


inside the loop.  

Even better, use the range-based/foreach style:  

    for (char character : string.toCharArray()) {


Now you don't have to worry about i or charAt at all.  

If you're using Java 8, you may want to look into the streaming API.  E.g. combining this post and this post.  

    return string.codePoints()
                 .filter( Character::isAlphabetic )
                 .collect( StringBuilder::new,
                           StringBuilder::appendCodePoint,
                           StringBuilder::append )
                 .toString();


I haven't tested this.  I just combined the two solutions to similar problems in a way that I believe does what you want.