3

I've seen some answers like this one
But none of them add a pad to both sides of a given string.

Example:

String input = "text";
System.out.println(add_pad(input,10,"#"));

Output: ###text###

What I am looking for is a way that add a pad to both sides of a string and takes as arguments the string and the character or characters to fill with... Maybe something like str_pad in php.

Community
  • 1
  • 1
Philip Enc
  • 1,007
  • 14
  • 21

3 Answers3

2

I suspect apache commons StringUtils will come to your rescue:

http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#center(java.lang.String,%20int,%20char)

e.g.

String input = "text";
System.out.println(StringUtils.center(input,10,"#"));
theINtoy
  • 2,815
  • 1
  • 31
  • 49
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Rizier123 Dec 15 '15 at 13:57
  • Thank you, but I can not include commons.apache library in my app for some security reasons... – Philip Enc Dec 15 '15 at 13:59
2

FULL IMPLEMENTATION OF THE ORIGINAL STR_PAD

Thanks to theINtoy user answer and I think it would be a good one (not tested yet),
but I ended up implementing the full str_pad php function in Java language, because all answers that I found has partial functionalities and I need more control over string like str_pad php does.

/**************************
*
* STR_PAD IMPLEMENTED
*
**************************/
public String str_pad(String input, int length, String pad, String    sense)
{
   int resto_pad = length - input.length();
   String padded = "";

   if (resto_pad <= 0){ return input; }

   if(sense.equals("STR_PAD_RIGHT"))
   {
       padded  = input;
       padded += _fill_string(pad,resto_pad);
   }
   else if(sense.equals("STR_PAD_LEFT"))
   {
       padded  = _fill_string(pad, resto_pad);
       padded += input;
   }
   else // STR_PAD_BOTH
   {
       int pad_left  = (int) Math.ceil(resto_pad/2);
       int pad_right = resto_pad - pad_left;

       padded  = _fill_string(pad, pad_left);
       padded += input;
       padded += _fill_string(pad, pad_right);
   }
   return padded;
}


protected String _fill_string(String pad, int resto )
{
   boolean first = true;
   String padded = "";

   if (resto >= pad.length())
   {
      for (int i = resto; i >= 0; i = i - pad.length())
      {
          if (i  >= pad.length())
          {
              if (first){ padded = pad; } else { padded += pad; }
          }
          else
          {
              if (first){ padded = pad.substring(0, i); } else { padded += pad.substring(0, i); }
          }
          first = false;
      }
  }
  else
  {
      padded = pad.substring(0,resto);
  }
  return padded;
}


USAGE - All possible test cases
============================

System.out.println(str_pad("coca cola 100", 10, "-", "STR_PAD_RIGHT"));
System.out.println(str_pad("coca cola", 10, "-",  "STR_PAD_RIGHT"));
System.out.println(str_pad("cocacola", 10, "-",   "STR_PAD_RIGHT"));
System.out.println(str_pad("cocacola", 10, "-",   "STR_PAD_LEFT"));
System.out.println(str_pad("cocacola", 10, "-",   "STR_PAD_BOTH"));
System.out.println(str_pad("cocacola", 15, "##",  "STR_PAD_RIGHT"));
System.out.println(str_pad("cocacola", 15, "##",  "STR_PAD_LEFT"));
System.out.println(str_pad("cocacola", 15, "##",  "STR_PAD_BOTH"));
System.out.println(str_pad("cocacola", 15, "&&&", "STR_PAD_RIGHT"));
System.out.println(str_pad("cocacola", 15, "&&&", "STR_PAD_LEFT"));
System.out.println(str_pad("cocacola", 15, "&&&", "STR_PAD_BOTH"));
System.out.println(str_pad("cocacola", 20, "***", "STR_PAD_BOTH"));
System.out.println(str_pad("cocacola", 20, "*-*", "STR_PAD_BOTH"));
System.out.println(str_pad("cocacola_light_taste_is_not_like_original", 20, ":::", "STR_PAD_BOTH"));

The first and last println will print all characters like do the original php str_pad function

Output:

System.out: coca cola 100
System.out: coca cola- 
System.out: cocacola-- 
System.out: --cocacola 
System.out: -cocacola- 
System.out: cocacola####### 
System.out: #######cocacola 
System.out: ###cocacola#### 
System.out: cocacola&&&&&&& 
System.out: &&&&&&&cocacola 
System.out: &&&cocacola&&&& 
System.out: ******cocacola****** 
System.out: *-**-*cocacola*-**-* 
System.out: cocacola_light_taste_is_not_like_original 

If someone find any good test or improvement, You are welcome to edit this answer for a better code...

Marco13
  • 50,927
  • 9
  • 71
  • 148
Philip Enc
  • 1,007
  • 14
  • 21
0

There are options using java.lang.String.format() and java.util.Formatter. http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html Essentially:

These options are discussed here:

How can I pad a String in Java?

Community
  • 1
  • 1
theINtoy
  • 2,815
  • 1
  • 31
  • 49