Jump to content

Transformar String (Replace)


Go to solution Solved by N3lson,

Recommended Posts

Posted (edited)

Boas eu sou iniciante no java, e estou perante um problema que é o seguinte.
Pretendo transformar uma String (query sql ) dependendo dos valores de entrada.

Até ao momento consegui fazer o seguinte
 

 String str = "SELECT t_nome FROM tprom001, tremi450 WHERE t_func=252";
        
  Pattern p = Pattern.compile("t\\D{4}\\d{3}");
  Matcher m = p.matcher(str);
 
 while (m.find()) {
           
  //System.out.println("t" + m.group() + "100");
   str = str.replaceAll("t\\D{4}\\d{3}", "t" + m.group() + "100");
  }

O que pretendia era transformar a string para ficar deste modo SELECT t_nome FROM ttprom001100, ttremi450100 where t_func=252 mantendo o valor a sublinhado e acrescentado os valores a negrito, mas o que esta a acontecer neste momento é que adiciona 2x o t e o 100

Edited by N3lson

You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

Posted (edited)

Coloquei desta forma, apesar de corrigir e ficar numa string mas repete o mesmo campo.
 

 String str = "SELECT t_nome FROM tprom001, tremi450 WHERE t_func=252";
        
          Pattern p = Pattern.compile("t\\D{4}\\d{3}");
          Matcher m = p.matcher(str);
         String x = null;
         while (m.find()) {
                   
           //System.out.println("t" + m.group() + "100");
           //str = str.replaceAll("t\\D{4}\\d{3}", "t" + m.group() + "100");
           
           //System.out.println(str.replaceAll("t\\D{4}\\d{3}", "t" + m.group() + "100"));
           x = str.replaceAll("t\\D{4}\\d{3}", "t" + m.group() + "100");
          }
          System.out.println(x);
Edited by N3lson
Update : Erro da string, não reparei adiciona o mesmo campo

You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

  • Solution
Posted

Resolvido, estava a dormir, alteração dentro do while

 x = str.replaceAll(mgroup(), "t" + m.group() + "100");

You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.