Skip to content

Instantly share code, notes, and snippets.

@Dimich7
Created October 23, 2015 19:16
Show Gist options
  • Save Dimich7/4a065a6c424524d501a3 to your computer and use it in GitHub Desktop.
Save Dimich7/4a065a6c424524d501a3 to your computer and use it in GitHub Desktop.
Given a string, take the first 2 chars and return the string with the 2 chars added at both the front and back, so "kitten" yields"kikittenki". If the string length is less than 2, use whatever chars are there.
public class Test {
public static void main(String[] args) {
System.out.println(method("hi"));
}
public static String method(String str){
if (str.length()<2)
return str+str+str;
String s=str.substring(0,2);
return s+str+s;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment