[Java] String padding

If you want to pad the string to the specified number of digits, use the ** "String.format" ** method.

String.format

Blank padding for strings

padding.java


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        String result5keta = blankPadding5keta();
        String result10keta = blankPadding10keta();
        System.out.println(result5keta);
        System.out.println(result10keta);
    }

    //Fill with 5 digits
    private static String blankPadding5keta(){
        String str = "123";
        String paddingStr = str.format("%5s", str);

        return paddingStr;
    }

    //Fill in blanks with 10 digits
    private static String blankPadding10keta(){
        String str = "12345";
        String paddingStr = str.format("%10s", str);

        return paddingStr;
    }
}

result


  12345
     12345

Zero padding of string

padding.java


import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        String result5keta = zeroPadding5keta();
        String result10keta = zeroPadding10keta();
        System.out.println(result5keta);
        System.out.println(result10keta);
    }

    //Fill with 0 in 5 digits
    private static String zeroPadding5keta(){
        String str = "123";
        String paddingStr = str.format("%5s", str).replace(" ", "0");
        
        return paddingStr;
    }

    //Fill with 0 in 10 digits
    private static String zeroPadding10keta(){
        String str = "12345";
        String paddingStr = str.format("%10s", str).replace(" ", "0");
        
        return paddingStr;
    }
}

result


00123
0000012345

Recommended Posts

[Java] String padding
Java string processing
Split string (Java)
[Java] String comparison and && and ||
Java string multiple replacement
[Note] Java: String search
[Note] Java: String survey
About Java String class
Reflection on Java string manipulation
[Java] About String and StringBuilder
Java
String
Java
[Java] Speed comparison of string concatenation
Various methods of Java String class
Arbitrary string creation code by Java
Studying Java 8 (String Joiner and join)
[Java] Correct comparison of String type
[Java] Divide a character string by a specified character
Java learning (0)
Studying Java ―― 3
[Java] array
[Java] Annotation
[Java] Module
Java array
Java tips, tips
Java date data type conversion (Date, Calendar, String)
Full-width → half-width conversion with Java String (full-width kana → half-width kana)
Java methods
Java method
java (constructor)
Regarding String type equivalence comparison in Java
Java array
[Java] ArrayDeque
java (override)
java (method)
String puzzle
Java Day 2018
java (array)
Java static
Java serialization
JAVA paid
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
[Java] Data type / string class cheat sheet
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
Notes on operators using Java ~ String type ~
[Java] Inheritance