terça-feira, 27 de julho de 2010

função Replace

function replace(s,velho,novo) {
nova_str = "";
cont=0;
tam= (s.length);
indice = tam - 1;
tam_velho = (velho.length);
indice_velho = tam_velho - 1;
if (tam < tam_velho ) {
nova_str = s;
} else {
while (cont <= indice-indice_velho) {
aux = "";
aux = s.substring(cont,cont+tam_velho);
if (aux == velho) {
nova_str += novo;
cont += (tam_velho);
if (cont > indice-indice_velho) {
nova_str += s.substring(cont,tam);
}
} else {
if (cont+tam_velho == tam) {
nova_str += s.substring(tam-tam_velho,tam);
} else{
nova_str += aux.charAt(0);
}
cont++;
}
}
}
return nova_str;
}

Nenhum comentário:

Postar um comentário