Java > String-1 > left2 (CodingBat Solution)

Problem:

Given a string, return a "rotated left 2" version where the first 2 chars are moved to the end. The string length will be at least 2.

left2("Hello") → "lloHe"
left2("java") → "vaja"
left2("Hi") → "Hi"


Solution:

public String left2(String str) {
  return str.substring(2, str.length()) + str.substring(0,2);
}


3 comments :

  1. public String left2(String str) {
    return str.substring(2) + str.charAt(0) + str.charAt(1);
    }

    ReplyDelete
  2. return str.substring(2) + str.substring(0,2);

    str.length()?? :O

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact