Friday, August 2, 2013

java.lang.String concat() tweak:


Java Doc says
   
        Concatenation happens  the specified string to the end of this string.
If the length of the argument string is 0, then this String object is returned. Otherwise, a new String object is created, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.


Saying that , how to implement our own concat().

Here is the sample:

 public static void concat(String firstString ,String secondString){

char [] firstStringarray = firstString.toCharArray();
char [] secondStringarray = secondString.toCharArray();
if(secondStringarray.length == 0){
System.out.println(firstString);
}else
{
       char [] newArray = new char[firstStringarray.length +                                       secondStringarray.length];
int index = 0;
while(index < firstStringarray.length){
newArray[index] = firstStringarray[index];
index ++;
}
while(index < newArray.length){
newArray[index] =         secondStringarray[secondStringarray.length (newArray.length - index)];                                 
index ++;
}
Sytem.out.println(new String(newArray));

}
}

No comments:

Post a Comment