Results 1 to 5 of 5
  1. #1
    maas is offline Member
    Join Date
    Jul 2010
    Posts
    37
    Rep Power
    0

    Default How to append characters to string

    Hi,

    I need your help in appending characters to a string in order to reach a specific length. In fact, I am having a dynamic value as an example String code = "55" or String code="555" and I want to append the number "0" to the left so the total length will be five "5". So the result will be "00055" in the first case and in the second case "00555", so how can I do that?

    I need to find a way to check the length of the string and then to append "0" to the left until reach five characters

    Thanks
    Last edited by maas; 04-29-2016 at 01:49 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to append characters to string

    Java Code:
    String val = "55";
    val = "000" + val;
    However, if you are doing that to format it prior to printing, you can do the following:
    Java Code:
    int val = 55;
    System.out.printf("%05d%n", val);
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    maas is offline Member
    Join Date
    Jul 2010
    Posts
    37
    Rep Power
    0

    Default Re: How to append characters to string

    Thanks but what I was looking for is different .The value will be dynamic and I can't append "000"+"55" to it. The value will differ as an example it will be "55" or "555", so I need a way to check the length and to append the "0" to the left so the total length will be five characters

  4. #4
    maas is offline Member
    Join Date
    Jul 2010
    Posts
    37
    Rep Power
    0

    Default Re: How to append characters to string

    Quote Originally Posted by jim829 View Post
    Java Code:
    String val = "55";
    val = "000" + val;
    However, if you are doing that to format it prior to printing, you can do the following:
    Java Code:
    int val = 55;
    System.out.printf("%05d%n", val);
    Regards,
    Jim
    Thanks but what I was looking for is different .The value will be dynamic and I can't append "000"+"55" to it. The value will differ as an example it will be "55" or "555", so I need a way to check the length and to append the "0" to the left so the total length will be five characters

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to append characters to string

    Java Code:
       
          String val = "55";
          for (int i = 0; i < 5; i++) {
             String newVal = String.format("%010d", Integer.valueOf(val));
             System.out.println(newVal);
             val += 5; // increase length of string by one digit.
          }
    Of course, if you use int values instead of Strings then you don't need to use the Integer.valueOf(val) method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Help with reversing the characters of a String!!
    By Valerie10b in forum New To Java
    Replies: 4
    Last Post: 03-12-2013, 06:09 AM
  2. Help - (Sorting characters in a string)
    By DanPe in forum New To Java
    Replies: 1
    Last Post: 05-12-2012, 12:57 AM
  3. append character in string '\'???
    By jspexception in forum New To Java
    Replies: 3
    Last Post: 02-23-2012, 12:51 AM
  4. how to get the characters one by one from a String?
    By Somitesh Chakraborty in forum New To Java
    Replies: 3
    Last Post: 08-20-2008, 09:56 PM
  5. Getting all characters in a String
    By Alayna in forum New To Java
    Replies: 2
    Last Post: 05-20-2007, 12:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •