Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to add leading zeros to the shorter string of two strings in Java

2 Answers

0 votes
public class Program {
    static String addToEmptyStr(String pad, int len) {
        String str = ""; 
         
        while (str.length() < len) {
            str += pad;
        }

        return str;
    }
    
    static String addLeadingZero(String str1, String str2) {
        int len1 = str1.length();
        int len2 = str2.length();
        
        String strToPad = "";

        int diff = Math.abs(len1 - len2);
  
        if (diff > 0) {
            strToPad = addToEmptyStr("0", diff);
            if (len1 > len2) { 
                strToPad += str2;
            } else {
                strToPad += str1;
            }
        }
        
        return strToPad;
    }

    public static void main(String[] args) {
        String str1 = "123";
        String str2 = "8976753";
        
        if (str1.length() > str2.length()) {
            str2 = addLeadingZero(str1, str2);
        } else {
            str1 = addLeadingZero(str1, str2);
        }

        System.out.println(new String(str1));
        System.out.println(new String(str2));
    }
}



/*
run

0000123
8976753

*/

 



answered May 26, 2024 by avibootz
0 votes
import java.util.Arrays;

public class Program {
    static String addToEmptyStr(String pad, int len) {
        char[] zeros = new char[len];
        Arrays.fill(zeros, '0');
        
        String emptyString = new String(zeros);
         
        return emptyString;
    }
     
    static String addLeadingZero(String str1, String str2) {
        int len1 = str1.length();
        int len2 = str2.length();
         
        String strToPad = "";
 
        int diff = Math.abs(len1 - len2);
   
        if (diff > 0) {
            strToPad = addToEmptyStr("0", diff);
            if (len1 > len2) { 
                strToPad += str2;
            } else {
                strToPad += str1;
            }
        }
         
        return strToPad;
    }
 
    public static void main(String[] args) {
        String str1 = "123";
        String str2 = "8976753";
         
        if (str1.length() > str2.length()) {
            str2 = addLeadingZero(str1, str2);
        } else {
            str1 = addLeadingZero(str1, str2);
        }
 
        System.out.println(new String(str1));
        System.out.println(new String(str2));
    }
}
 
 
 
/*
run
 
0000123
8976753
 
*/


 



answered May 26, 2024 by avibootz

Related questions

...