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:

Snowflake - Cost Optimization

Reduce auto-suspend to 60 seconds Reduce virtual warehouse size Ensure minimum clusters are set to 1 Consolidate warehouses Separate warehou...