473,804 Members | 3,037 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to add leading zeros to String

3 New Member
Hi friends,

i need help ..

i am getting String value and i need to add leading zeros..
total length of Target string is 13char

eg: suppose if i get 12 as values..
it needs to convert into 0000000000012

suppose if i get 125 .it needs convert into 0000000000125

target string should be 13char..

we need to add leading zeros to the source string to make result string to 13 char and result string needs to retun

please help me

Ram
May 11 '07 #1
11 45634
dmjpro
2,476 Top Contributor
welcome to TSDN.
this site is not for post the code from begining.
if u have a problem with ur code then the experts r here to correct ur code.
soooooo........ ........

how much u did?

try to write the code ur self.
then post that code if u facing any problem.

best of luck.
kind regards.

dmjpro.
May 11 '07 #2
ram23
3 New Member
hi thanks for your reply

i am new java,,,

i tried with this code...

String addZeros(Sting a)
{
int i=0;
i=a.length();

if ( i = 13 )
return a;
else
{
int j= 13 - i;
for (int k=0; k<=j; j++)
{a="0"+a;}
return a;
}
}


my systems getting hanging....
please guide
May 11 '07 #3
rsrinivasan
221 New Member
Hi,

Im beginers for java.

I write to add 0's in leading space. try it and give ur comments whether it satisfies ur needs or not.

Expand|Select|Wrap|Line Numbers
  1. public class ArrayCheck 
  2. {
  3.     public static void main(String[] args) 
  4.     {
  5.         String str="";
  6.         int temp;
  7.         str="125";
  8.         temp = 13-str.length();
  9.         System.out.println("Length: "+temp);
  10.         for(int i=0;i<temp;i++)
  11.         {
  12.             str="0"+str;
  13.         }
  14.         System.out.println(str);
  15.     }
  16. }    

Thanks,
Srinivasan r.
May 11 '07 #4
rsrinivasan
221 New Member
Hi,
In ur code remove '=' with '=='. Then try to run this program.Also delete '=' in for loop..

Thanks,
Srinivasan r.
May 11 '07 #5
rsrinivasan
221 New Member
Hi,

I fix the problem in ur code.

Why it is hanging is you did not increment 'k' value.

The Correct code is

String addZeros(String a)
{
int i=0;
i=a.length();
if ( i == 13 )
return a;
else
{
int j= 13 - i;
for (int k=0; k<j; k++)
{
a="0"+a;
}
return a;
}
}

Run it...

Thanks..
Srinivasan r.
May 11 '07 #6
ram23
3 New Member
Hi Srinvasan..

thanks for finding my poblem...
i given j++ instead of k++

my problem solved..

thanks for your help

regards
Ram
May 11 '07 #7
JosAH
11,448 Recognized Expert MVP
Here's a little trick: you don't need a loop at all; have a look:
Expand|Select|Wrap|Line Numbers
  1. String prependZeros(int number) { // number >= 0
  2.    String s= "000000000000"+number; // twelve zeros prepended
  3.    return s.substring(s.length()-13); // keep the rightmost 13 chars
  4. }
kind regards,

Jos
May 12 '07 #8
sateesht
41 New Member
Hi,

public class StringTest
{
public static void main(String[] args)
{
String str="0000000000 000";
String str1="abcd";
String str2=str+str1;
int k=str1.length() ;
System.out.prin tln(str2.substr ing(k,str2.leng th()));
}
}


Cheers,
Sateesh.
May 14 '07 #9
princessmarquez
1 New Member
to: JosAH

thanks for a two lines of code that solved the problem :-)
Jun 7 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
13789
by: david | last post by:
Hi, I have an application as follows: MySQL database Back-Eend linked to MS Access Front-End and ASP Web Application. I require users to enter Serial Numbers such as: 0105123567 (10 digits), the first 4 being the month and year (mmyy)
7
2911
by: david | last post by:
Hi, I have 2 text boxes on an ASP form. A user enters a Serial Number in TB1 such as 0105123456, presses tab to move to TB2, TB2 then displays the value of TB1 after a calculation has been done. (Based on a Serial Number range). i.e. a user enters 0105123456, TB2 then adds 'x' qty to this number depending on how many serial numbers are required.
5
11515
by: samik_tanik | last post by:
I need to export a datagrid to Excel. I could did this. But, also need to keep the leading zeros in the data. How can I acheive this? Any help would be appreciated. -- Thanking you in anticipation, Regards,
6
19204
by: Rich Raffenetti | last post by:
How can one format an integer into a hex string with leading zeros? Suppose an integer is 512 which in Hex is 200. I wish to print the 4-byte integer as 0200 or even 0x0200. The HEX function doesn't create leading zeros. The Format function (format(value,"X") doesn't create leading zeros. I believe there should be a simple way that doesn't involving measuring and padding the string.
7
12307
by: leninv | last post by:
Hi, I have the following code where 'recs' is a record set. For i=0 to recs.Fields.Count - 1 if i = 0 then pindnt = string(itmlvl*2," ") response.write pindnt & recs(i) else Response.write recs(i)
6
7768
by: JimmyKoolPantz | last post by:
Task: Customer wants a script of the data that was processed in a "CSV" file. Problem: Zip-Code leading zeros are dropped Basically we have a client that has requested a custom script for each file that he has us process. He wants this in a Comma Delimited Format.
1
27836
by: LFM | last post by:
I have a database setup that uses ODBC connections into SQL for read only views to compare information from multiple sql databases in our company. Most of the tables use the Employee ID as the primary key. The problem is, one database formats the EmpID with leading zeros so all EmpID's are 5 characters long. (ie, 00123, 01523) Another database formats the EmpID as just the number of characters as the number is (ie, 123, 1523). When you...
0
4108
by: Monty | last post by:
Hi All, I am having a problem with leading zeros being stripped from fields in a CSV file when I bring them in using Jet/OleDB. In VB.Net/VS 2008, I am accessing a CSV file like so: sSQL = "SELECT * FROM " sConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _ Microsoft.VisualBasic.FileIO.FileSystem.GetParentPath(msFile) & _
2
10667
by: Smokey Grindel | last post by:
I basically need to remove any leading zeros and hyphens from a string... regex seemed like the best rout and using a replace regex method... anyone know of any good strings for this? The one I just made quick is ^0+| but not having much experience with regex, is this the correct one?
0
10571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10326
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.