Infix notation in functions

Infix notations can be used to define functions that can be used, such as 1 + 2, 2 - 3, true && false, a == b statements where there is an operator and two operands. The following function with the infix notation can be used to ZIP (interweave) words together in a way that the letters of each String are joined together to create a new String that has overlapping letters:

infix fun String.zip(s1 : String) : String {
var result : String = "";
var zipLength : Int = Math.min(s1.length, this.length);
for(i in 0..zipLength-1) {
result += this[i];
result += s1[i];
}
return result;
}

 The preceding function can be called as follows:

println("acegikmoqsuwy" zip "bdfhjlnprtvxz");

This will produce the following string as a result:

 abcdefghijklmnopqrstuvwxyz
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.66.13