Sunday, 7 April 2013

String pad in java


public class Trial {
    public static void main(String arg[])
    {
        String password="password";
        String padded=String.format("%"+password.length()+"s","").replace(" ", "*");
        System.out.println(padded);
    }
}

Output:


run:
********
BUILD SUCCESSFUL (total time: 0 seconds)


Different types:


     String.format("%10s", "foo").replace(' ', '*');
     String.format("%-10s", "bar").replace(' ', '*');
     String.format("%10s", "longer than 10 chars").replace(' ', '*');

     Output:

        *******foo
        bar*******
        longer*than*10*chars



No comments:

Data Sharing

  1. Create Share CREATE SHARE my_share; 2. Grant privileges to share GRANT USAGE ON DATABASE my_db TO SHARE my_share; GRANT USAGE ...