Posted by & filed under Code Tips.

private String rotateString(String s, int distance) {
    String out = "";
    for (int i=0; i<s.length(); i++) {
        out += s.charAt((i + distance) % s.length()); 
    }
    return out; 
}

This comes from needing a quick and easy way to rotate a String in Java.

Leave a Reply

Your email address will not be published. Required fields are marked *