Jump to content
Sign in to follow this  
N3lson

Transformar String (Replace)

Recommended Posts

N3lson

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.

Share this post


Link to post
Share on other sites
N3lson

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.

Share this post


Link to post
Share on other sites
N3lson

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.

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×
×
  • 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.