4

Friends I have to impliment something in project, I found some difficulties, and is as follows:

String name1 = "Bharath"  // its length should be 12
String name2 = "Raju"     //  its length should be 8
String name3 = "Rohan"    //  its length should be 9
String name4 = "Sujeeth"   //  its length should be 12
String name5 = "Rahul"  //  its length should be 11  " Means all Strings with Variable length"

I have the Strings and their Lengths.I need to get output as in the below format.By using string concatination and padding.I need answer in Groovy, Even if Java also it is fine..

"Bharath     Raju    Rohan    Sujeeth     Rahul     "

Means:

Bharath onwards 5 black spaces as lenth is 12 (7+5 = 12),

Raju onwards 4 black spaces as lenth is 8 (4+4 = 8),

Rohan onwards 4 black spaces as lenth is 9(5+4),

Sujeeth onwards 5 black spaces as lenth is 12 (7+5),

Rahul onwards 6 black spaces as lenth is 11(5+6),

tim_yates
  • 154,107
  • 23
  • 313
  • 320
Bharath A N
  • 397
  • 3
  • 5
  • 11

4 Answers4

8

You could do this:

// A list of names
def names = [ "Bharath", "Raju", "Rohan", "Sujeeth", "Rahul" ]

// A list of column widths:
def widths = [ 12, 8, 9, 12, 11 ]

String output = [names,widths].transpose().collect { name, width ->
  name.padRight( width )
}.join()

Which makes output equal to:

'Bharath     Raju    Rohan    Sujeeth     Rahul      '

Assuming I understand the question... It's quite hard to be sure...

tim_yates
  • 154,107
  • 23
  • 313
  • 320
3

Take a look at Apache's StringUtils. It has methods to pad with spaces (either left or right).

npinti
  • 50,175
  • 5
  • 67
  • 92
  • 3
    No need for another dependency if you're using Groovy... it has `padLeft` and `padRight` already as [metaClass methods for String](http://groovy.codehaus.org/groovy-jdk/java/lang/String.html#padLeft%28java.lang.Number%29) – tim_yates Jul 23 '12 at 13:01
  • 1
    @tim_yates: I usually find the Util classes quite handy and would recommend them. That being said however, if it is only for this operation, I would agree with you on avoiding extra dependencies. – npinti Jul 23 '12 at 13:50
3

You can use sprintf, which is added to the Object class so it is always available:

def s = sprintf("%-12s %-8s %-9s %-12s %-11s", name1, name2, name3, name4, name5)

assert s == "Bharath      Raju     Rohan     Sujeeth      Rahul      "

The formatting string used with sprintf is the same as the format string used for the Formatter class. See the JDK documentation for the format string for more information.

astimony
  • 111
  • 2
2

As already said, you can use String.format() method to achieve your goal.

For example :

    String[] strings = {
            "toto1",
            "toto22",
            "toto333",
            "toto",
            "totoaa",
            "totobbb",
            "totocccc",
    };
    String marker = "01234567890|";
    String output = "";
    for(String s : strings) {
        output += marker;
    }
    System.out.println(output);
    output = "";
    for(String s : strings) {
        output += String.format("%-12s", s);
    }
    System.out.println(output);

This will output a first line with markers for 12 chars then 2nd line with expected string :

01234567890|01234567890|01234567890|01234567890|01234567890|01234567890|01234567890|
toto1       toto22      toto333     toto        totoaa      totobbb     totocccc    
Xvolks
  • 1,919
  • 1
  • 16
  • 26