Tenho que fazer uma formatação em uma string do tipo: “abc123”
a saida deve ser:
“abc<|>123”
ou seja , quando não houver “href” dentro da tag, deve-se substituir o que houvber dentro da tag por " | "
Eu fiz um programa da seguinte forma::
package teste2;
public class Main
{
public Main ()
{
}
public static void main (String[] args)
{
String str="cvxcv<abchref>cvxcv";
Boolean href=false;
int abre=str.indexOf ("<"),ind, fecha=str.indexOf (">"),
ultimoFecha=str.lastIndexOf (">"),ultimoAbre=str.lastIndexOf ("<");
String saida="";
ind=abre;
System.out.printf("%d",str.length ());
while (ind<=ultimoFecha)
{
String aux="";
if(str.length ()>fecha)
{
aux=aux.concat(str.substring(abre,fecha+1));
}
else
{
aux=aux.concat (str.substring (abre));
}
StringBuffer strbuffer=new StringBuffer (aux);
for(int i=abre;i<fecha-3;i++)//verifica se entre as chaves há href
{
if (aux.regionMatches (true,i,"href",0,4))
{
href=true;
}
}
if (href)//Caso haja href
{
saida=saida.concat (aux);
}
else//Caso não haja href
{
saida=saida.concat (strbuffer.replace (abre+1,fecha,"|").toString ());
}
abre=str.indexOf ("<",abre+1);
fecha=str.indexOf (">",fecha+1);
ind=fecha;
href=false;
}
System.out.print (saida);
}
}
O problema que dá " String Out Of Range:-1" , na linha 30, alguém pode me ajudar?