3

I have two Strings.

String a = "Abraham"
String b = "Best Friend"

I want an output similar to this:

Abraham.......OK
Best Friend...OK

I used String.format() to get the following result.

a = String.format("%0$-" + b.lenght() + "s   ", a);
b = String.format("%0$-" + b.lenght() + "s   ", b);

Abraham       OK
Best Friend   OK

I can not use String.replace(), because the space between "Best" and "Friend" would be replaced as well.

I found a solution for putting zeroes in front of the beginning of the String. However, i dont understand in which way I should modify this solution to get the desired output.

answer by anubhava

String sendID = "AABB";
String output = String.format("%0"+(32-sendID.length())+"d%s", 0, sendID);

I found solutions with padded Strings, but i would like to solve this with the String.format()-Method.

Community
  • 1
  • 1
leif_good
  • 366
  • 4
  • 17
  • might be something here: http://stackoverflow.com/questions/388461/how-can-i-pad-a-string-in-java – Moob Apr 08 '16 at 12:10
  • I'd not use `String.format()` here but use a `StringBuilder` and add the dots in a loop. – Thomas Apr 08 '16 at 12:10

4 Answers4

4

You can use replaceAll() with a regex like this:

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String arr[] = {"Abraham", "Best Friend"};
        for(String s:arr)
            System.out.println(String.format("%-"+32+"s", s).replaceAll("\\s(?=\\s+$|$)", ".")+"OK");
    }
}

Output:

Abraham.........................OK
Best Friend.....................OK

http://ideone.com/Rb886w

riteshtch
  • 8,189
  • 4
  • 19
  • 35
2

I'd probably use a loop for that (along with a StringBuilder for performance reasons:

public String pad(String source, int targetLength, String pad) {
  StringBuilder result = new StringBuilder( source );
  for( int i = source.length(); i < targetLength; i+=pad.length() ) {
    result.append(pad);
  }
  return result.toString();
}

//calling:
a = pad( a, 32, "." );

Note that this would stop early if targetLength - source.length() is not a multiple of pad.length(). To fix that either only pass single characters or handle the last part by using pad.substring(...) with appropriate values.

Edit:

Here's a version with pad.subString(...):

public String pad(String source, int targetLength, String pad) {
  StringBuilder result = new StringBuilder( source );    
  while( result.length() < targetLength ) {
    result.append(pad.substring( 0, Math.min( pad.length(), targetLength -  result.length() ) ));
  }
  return result.toString();
}
Thomas
  • 80,843
  • 12
  • 111
  • 143
  • The solution with StringBuilder seems to be very fitting for my usecase. – leif_good Apr 08 '16 at 12:24
  • In my usecase, pad should allways be a single character, therefore i decided to make it a character and then "cast"(concatenate) it to a String. – leif_good Apr 08 '16 at 13:43
  • 1
    @replayleif there's no need for the "cast", `StringBuilder` has a `append(char)` method. – Thomas Apr 08 '16 at 13:46
1

A simple recursive method to do it would be:

public static String fill(String s, int l){
    if(s.length()==l)
        return s; // Return the String , as the task is complete
    else 
        return fill(s+".",l); // Append 1 .(dot) and again call the method
}

This is the simplest way I can think of.

So, for you, it would be:

a = fill(a,b.length());
b = fill(b,b.length());
dryairship
  • 5,702
  • 2
  • 27
  • 52
  • Just as a sidenode: if `l` is large and the initial string is short this can lead to serious memory problems since each recursion will create at least one intermediate string. Thus I'd suggest using a `StringBuilder` internally, i.e. for the actual recursive calls. – Thomas Apr 08 '16 at 13:50
0

Just a thought..

public class Test {
  public static void main(String[] args) {
    String TEN_DOTS = "..........";
    String TEST_STR = "foo";
    String outstr = TEST_STR + TEN_DOTS;
    System.out.println(outstr.substring(0,10));
  }
}

Outputs:

foo.......
unigeek
  • 2,045
  • 21
  • 22