Here's one simple way. Remember strings begin at index 0;
public class StringTest{
public StringTest(){ }
public static String substitute(String str1, String str2){
StringBuffer sb = new StringBuffer();
sb.append(str1.substring(0,2));
sb.append(str2.substring(2,5));
sb.append(str1.substring(5));
return sb.toString();
}
public static void main(String []agrs){
String input1 ="123 678 8910";
String input2 ="000 111 0000 0000";
String result = StringTest.substitute(input1, input2);
System.out.println("Result: " + result);
}
}
public class StringTest{
public StringTest(){ }
public static String substitute(String str1, String str2){
StringBuffer sb = new StringBuffer();
sb.append(str1.substring(0,2));
sb.append(str2.substring(2,5));
sb.append(str1.substring(5));
return sb.toString();
}
public static void main(String []agrs){
String input1 ="123 678 8910";
String input2 ="000 111 0000 0000";
String result = StringTest.substitute(input1, input2);
System.out.println("Result: " + result);
}
}
Comments
Post a Comment