eu ja consegui fazer a leitura do arquito txt, e armazenar tudo em um String chamado ( lerLinha ), o problema é:
eu quero substituir em cada linha o terceiro " | " (pipe) pelo numero 5, eu ja utilizei o meto StringTokenizer, Substring… e não estou conseguindo,
entao agora estou utilizando um novo metodo para tentar substituir mais nao estou tendo sucesso!
segue o metodo
[code] public void substituirCaracteres() {
StringBuffer strb = new StringBuffer(lerLinha);
strb.setCharAt(3, ‘5’);
System.out.println(strb);
}[/code]
se alguem puder me ajudar a resolver esse problema eu agradeço… obrigado!
String teste = "|I050|01012002|01|S|04|1112|111|BANCO MOVIMENTO|";
teste = teste.replaceAll("(\\|I\\d+\\|\\d+)\\|(.*)", "$15$2");
System.out.println(teste);
[code] //Apenas para usuário não precisar passar parâmetros "desnecessários"
int getIndex(String str, String search, int depth) {
return getIndex(str, search, 0, depth, 0);
}
int getIndex(String str, String search, int from, int depth, int count) {
if(str.length() < from || depth == 0) return -1;
int index = str.indexOf(search, from);
if(++count >= depth) return index; //Para não precisar entrar outro nível para retornar
//Continua busca a partir do indice seguinte
return getIndex(str, search, index + 1, depth, count);
}
[/code]
USO
[code]
String t = “parte1|parte2|parte3|parte4”;
String change = "5";
int indice = getIndex(t, "|", 3); //Altere o último parâmetro para o pipe que precisa
if(indice > 0) {
String init = t.substring(0, indice);
String end = t.substring(indice+1);
System.out.println(init+change+end);
} else {
System.out.println("No pipe found");
}[/code]