Retirar zeros de String ? [RESOLVIDO]  XML
Índice dos Fóruns » Java Básico
Autor Mensagem
Metaleiro
Virtual Machine Man
[Avatar]

Membro desde: 06/07/2006 10:55:54
Mensagens: 671
Localização: São Bernardo do Campo - SP
Offline

Alguém sabe como retirar os zeros a esquerda de uma String ?

00000062626262626266

Se alguém puder ajudar desde agradeço !

This message was edited 1 time. Last update was at 25/11/2011 11:34:03


"Why, Mr. Anderson? Why do you do it? Why get up? Why keep fighting? Do you believe you're fighting for something? For more than your survival? Can you tell me what it is? Do you even know? Is it freedom? Or truth? Perhaps peace? Yes? No? Could it be for love? Illusions, Mr. Anderson. Vagaries of perception. The temporary constructs of a feeble human intellect trying desperately to justify an existence that is without meaning or purpose. And all of them as artificial as the Matrix itself, although only a human mind could invent something as insipid as love. You must be able to see it, Mr. Anderson. You must know it by now. You can?t win. It?s pointless to keep fighting. Why, Mr. Anderson? Why? Why do you persist?"

Neo :
-Because is my choice

asousaj
JavaChild
[Avatar]

Membro desde: 03/07/2011 18:19:45
Mensagens: 120
Localização: Campo Grande/MS
Offline

...

This message was edited 1 time. Last update was at 25/11/2011 10:50:24


Adriano Sousa de Jesus
OCJP em 2012



[Email] [MSN]
Metaleiro
Virtual Machine Man
[Avatar]

Membro desde: 06/07/2006 10:55:54
Mensagens: 671
Localização: São Bernardo do Campo - SP
Offline

Tenho uma String e gostaria de retirar os zeros a esquerda:


00000062626262626266



62626262626266


Desde já agradeço a quem puder ajudar.

"Why, Mr. Anderson? Why do you do it? Why get up? Why keep fighting? Do you believe you're fighting for something? For more than your survival? Can you tell me what it is? Do you even know? Is it freedom? Or truth? Perhaps peace? Yes? No? Could it be for love? Illusions, Mr. Anderson. Vagaries of perception. The temporary constructs of a feeble human intellect trying desperately to justify an existence that is without meaning or purpose. And all of them as artificial as the Matrix itself, although only a human mind could invent something as insipid as love. You must be able to see it, Mr. Anderson. You must know it by now. You can?t win. It?s pointless to keep fighting. Why, Mr. Anderson? Why? Why do you persist?"

Neo :
-Because is my choice

Frantic Avenger
Java Ninja
[Avatar]

Membro desde: 19/05/2009 15:01:42
Mensagens: 258
Localização: RJ
Offline

Tendou o replaceAll?


No "Insere aqui" deixa como "" (nulo)

Homer Simpson: "Nunca diga qualquer coisa a não ser que tenha certeza que todo mundo pensa o mesmo."

George Orwell: "Se a liberdade significa alguma coisa, será sobretudo o direito de dizer às outras pessoas o que elas não querem ouvir."
Metaleiro
Virtual Machine Man
[Avatar]

Membro desde: 06/07/2006 10:55:54
Mensagens: 671
Localização: São Bernardo do Campo - SP
Offline

É que o replaceAll retira todos os Zeros da String e pode ser assim

00000666007770009


Mas agradeço !

"Why, Mr. Anderson? Why do you do it? Why get up? Why keep fighting? Do you believe you're fighting for something? For more than your survival? Can you tell me what it is? Do you even know? Is it freedom? Or truth? Perhaps peace? Yes? No? Could it be for love? Illusions, Mr. Anderson. Vagaries of perception. The temporary constructs of a feeble human intellect trying desperately to justify an existence that is without meaning or purpose. And all of them as artificial as the Matrix itself, although only a human mind could invent something as insipid as love. You must be able to see it, Mr. Anderson. You must know it by now. You can?t win. It?s pointless to keep fighting. Why, Mr. Anderson? Why? Why do you persist?"

Neo :
-Because is my choice

asousaj
JavaChild
[Avatar]

Membro desde: 03/07/2011 18:19:45
Mensagens: 120
Localização: Campo Grande/MS
Offline

Não sei se é gamgiarra assim, mas funciona

This message was edited 2 times. Last update was at 25/11/2011 11:18:38


Adriano Sousa de Jesus
OCJP em 2012



[Email] [MSN]
Frantic Avenger
Java Ninja
[Avatar]

Membro desde: 19/05/2009 15:01:42
Mensagens: 258
Localização: RJ
Offline

Metaleiro, não sei se vai servir porque nunca usei esse método, mas olha na API, não posso testar aqui por questão de tempo, mas talvez ajude. Só tem que saber expressão regular



Espero ter ajudado.

Homer Simpson: "Nunca diga qualquer coisa a não ser que tenha certeza que todo mundo pensa o mesmo."

George Orwell: "Se a liberdade significa alguma coisa, será sobretudo o direito de dizer às outras pessoas o que elas não querem ouvir."
Metaleiro
Virtual Machine Man
[Avatar]

Membro desde: 06/07/2006 10:55:54
Mensagens: 671
Localização: São Bernardo do Campo - SP
Offline

asousaj wrote:Não sei se é gamgiarra assim, mas funciona




Muito obrigado a todos.

Agradeço toda a ajuda !

O método funcionou perfeitamente !

"Why, Mr. Anderson? Why do you do it? Why get up? Why keep fighting? Do you believe you're fighting for something? For more than your survival? Can you tell me what it is? Do you even know? Is it freedom? Or truth? Perhaps peace? Yes? No? Could it be for love? Illusions, Mr. Anderson. Vagaries of perception. The temporary constructs of a feeble human intellect trying desperately to justify an existence that is without meaning or purpose. And all of them as artificial as the Matrix itself, although only a human mind could invent something as insipid as love. You must be able to see it, Mr. Anderson. You must know it by now. You can?t win. It?s pointless to keep fighting. Why, Mr. Anderson? Why? Why do you persist?"

Neo :
-Because is my choice

bezier curve
JavaEvangelist
[Avatar]
Membro desde: 28/11/2009 17:55:58
Mensagens: 411
Offline

Retirar zeros à esquerda de uma string requer que você se lembre do caso em que a string é totalmente zerada ("00000000000000").

Pelo que imagino, nesse caso você teria de deixar um zero - ficaria: "0", não "vazio" como essas soluções que foram apresentadas aqui.

Normalmente eu converteria isso para long, e então converteria de volta, mais ou menos como:


Isso também leva em conta o caso "-00000002345" - note que vocês também não pensaram em números negativos, não?

Frantic Avenger
Java Ninja
[Avatar]

Membro desde: 19/05/2009 15:01:42
Mensagens: 258
Localização: RJ
Offline

bezier curve wrote:
Isso também leva em conta o caso "-00000002345" - note que vocês também não pensaram em números negativos, não?


Não tá na especificação a gente não considera
Senão depois se e somente se der errado eles dizem:
Foi ideia do programador!

Homer Simpson: "Nunca diga qualquer coisa a não ser que tenha certeza que todo mundo pensa o mesmo."

George Orwell: "Se a liberdade significa alguma coisa, será sobretudo o direito de dizer às outras pessoas o que elas não querem ouvir."
asousaj
JavaChild
[Avatar]

Membro desde: 03/07/2011 18:19:45
Mensagens: 120
Localização: Campo Grande/MS
Offline

Inclusive como não foi especificado pode até vir "bohemia", só foi dito que seria uma String, heim? kkkk

Adriano Sousa de Jesus
OCJP em 2012



[Email] [MSN]
Metaleiro
Virtual Machine Man
[Avatar]

Membro desde: 06/07/2006 10:55:54
Mensagens: 671
Localização: São Bernardo do Campo - SP
Offline

Para o meu caso funciona perfeitamente.

É específico.

A String virá numérica com zeros a esquerda, nunca valores negativos.

Exemplo :

000999

assim :

999

0007770001

assim :

7770001

O programa já garante que virá em formato numérico.

Obrigado a todos.

"Why, Mr. Anderson? Why do you do it? Why get up? Why keep fighting? Do you believe you're fighting for something? For more than your survival? Can you tell me what it is? Do you even know? Is it freedom? Or truth? Perhaps peace? Yes? No? Could it be for love? Illusions, Mr. Anderson. Vagaries of perception. The temporary constructs of a feeble human intellect trying desperately to justify an existence that is without meaning or purpose. And all of them as artificial as the Matrix itself, although only a human mind could invent something as insipid as love. You must be able to see it, Mr. Anderson. You must know it by now. You can?t win. It?s pointless to keep fighting. Why, Mr. Anderson? Why? Why do you persist?"

Neo :
-Because is my choice

 
Índice dos Fóruns » Java Básico
Ir para:   
Powered by JForum 2.1.8 © JForum Team