• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deleting first digit from a number  RSS feed

 
Mainakgh Ghosh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


I am new to java programming.
I am facing a problem in programming. In my program I have a number "012012" in mm/yyyy format, so my requirement is to delete the 0 from the beginning.

It means that from the month January to September the format will show like 12012, 22012, not like 012012, 022012.

So can any one please help me regarding the same. It is very urgent for me.


Thanks
Mainak
 
Stephan van Hulst
Saloon Keeper
Posts: 7963
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there, welcome to CodeRanch!

If I were you, I'd use a SimpleDateFormat to first parse() the String, and a second one to format() the resulting Date.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing you don't actually have a number, but a String. If so, it should be easy to test if the first character is '0', and if so, get the substring from the second character to the end.
 
Mainakgh Ghosh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephan,

Thanks for your reply..

My primary goal is to remove 0 from the front but not that particular digit. If the date is Oct 2012, it will show like 102012. And the data is predefined. I have to fetch
those numbers from a table and remove 0's if it is starting with.

and if you could show me with an example so it will be great.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mainakgh Ghosh wrote:It is very urgent for me.

Please EaseUp. There is no such thing as urgent around here.

If you know that the Strings will be correctly formatted, you can check the length() of the String to determine whether or not to get a substring of that String.
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your String values are predefined and consistent in their format, you can use some of the String methods to get what you want; check if charAt(0) == '0' and if yes, then get substring(1) to "remove" the first digit.
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also parse the Strings to an int, which loses leading 0s. Beware: this method depends on the Strings always having the same format.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!