Overlay Strings In Java While Skipping Spaces -


i want overlay 2 strings in java make 1 string. have method this, not want.

for example, if tell overlay "hello world" " hi", " hiorld".

i want able overlay "hello world" " hi", , "hellohiorld"!

here method using:

public static string overlaystring(string str, string overlay, int start, int end) {           if (str == null) {               return null;           }           if (overlay == null) {               overlay = "";           }           int len = str.length();           if (start < 0) {               start = 0;           }           if (start > len) {               start = len;           }           if (end < 0) {               end = 0;           }           if (end > len) {               end = len;           }           if (start > end) {               int temp = start;               start = end;               end = temp;           }           return new stringbuffer(len + start - end + overlay.length() + 1)               .append(str.substring(0, start))               .append(overlay)               .append(str.substring(end))               .tostring();       } 

how method if wanted method avoid replacing characters spaces, still keep positioning of text?

i can thing of 2 ways might able achieve this...

first...

public static string overlay1(string value, string with) {     string result = null;     if (value.length() != with.length()) {         throw new illegalargumentexception("the 2 string's must same length");     } else {         stringbuilder sb = new stringbuilder(value);         (int index = 0; index < value.length(); index++) {             char c = with.charat(index);             if (!character.iswhitespace(c)) {                 sb.setcharat(index, c);             }         }         result = sb.tostring();     }     return result; } 

which allows pass strings of same length , replace "non-whitespace" content of second in first, like...

overlay1("hello world",           "      hi   ")); 

or specify location want string overlaied, example...

public static string overlay2(string value, string with, int at) {     string result = null;     if (at >= value.length()) {         throw new illegalargumentexception("insert point beyond length of original string");     } else {         stringbuilder sb = new stringbuilder(value);         // assuming straight replacement out needing          // check white spaces...otherwise can use         // same type of loop first example         sb.replace(at, with.length(), with);         result = sb.tostring();     }     return result; }  overlay2("hello world",           "hi",           5));