Guest User

Untitled

a guest
Oct 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. package com.n1nja.utils;
  2. /**
  3. * Useful classes for manipulating {@link java.lang.String String}s.
  4. * @author Colin Haber
  5. * @version 0.0.0
  6. */
  7. public class Strings {
  8. /**
  9. * The default {@code char} used for padding
  10. * {@link java.lang.String String}s.
  11. * @since 0.0.0
  12. * @see #pad(String, int, String, boolean)
  13. */
  14. private static char DEFAULT_PAD_CHAR = ' ';
  15. /**
  16. * The default side to pad from.
  17. * @since 0.0.0
  18. * @see #pad(String, int, String, boolean)
  19. */
  20. private static boolean DEFAULT_PAD_SIDE = Strings.PAD_LEFT;
  21. /**
  22. * Indicates padding should be done from the left.
  23. * @since 0.0.0
  24. * @see #pad(String, int, String, boolean)
  25. */
  26. public static final boolean PAD_LEFT = true;
  27. /**
  28. * Indicates padding should be done from the right.
  29. * @since 0.0.0
  30. * @see #pad(String, int, String, boolean)
  31. */
  32. public static final boolean PAD_RIGHT = false;
  33. /**
  34. * Pads a {@link java.lang.String String} from the left with spaces.
  35. * @since 0.0.0
  36. * @param s the {@link java.lang.String String} to pad
  37. * @param len the amount of spaces to pad with
  38. * @return the padded {@link java.lang.String String}
  39. */
  40. public static String pad(String s, int len) {
  41. return Strings.pad(s, len, Strings.DEFAULT_PAD_CHAR);
  42. }
  43. /**
  44. * Pads a {@link java.lang.String String} from the left with the provided
  45. * {@code char}.
  46. * @since 0.0.0
  47. * @param s the {@link java.lang.String String} to pad
  48. * @param len the amount of {@code char}s to pad with
  49. * @param pad the {@code char} to pad with
  50. * @return the padded {@link java.lang.String String}
  51. */
  52. public static String pad(String s, int len, char pad) {
  53. return Strings.pad(s, len, Character.toString(pad));
  54. }
  55. /**
  56. * Pads a {@link java.lang.String String} from the left with the provided
  57. * {@code char}.
  58. * @since 0.0.0
  59. * @param s the {@link java.lang.String String} to pad
  60. * @param len the amount of {@code char}s to pad with
  61. * @param pad the {@link java.lang.String String} to pad with
  62. * @return the padded {@link java.lang.String String}
  63. */
  64. public static String pad(String s, int len, String pad) {
  65. return Strings.pad(s, len, pad, Strings.DEFAULT_PAD_SIDE);
  66. }
  67. /**
  68. * Pads a {@link java.lang.String String} from the left with the provided
  69. * {@code char}.
  70. * @since 0.0.0
  71. * @param s the {@link java.lang.String String} to pad
  72. * @param len the amount of {@code char}s to pad with
  73. * @param side the side to pad from
  74. * @return the padded {@link java.lang.String String}
  75. */
  76. public static String pad(String s, int len, boolean side) {
  77. return Strings.pad(s, len, Strings.DEFAULT_PAD_CHAR, side);
  78. }
  79. /**
  80. * Pads a {@link java.lang.String String} from the left with the provided
  81. * {@code char}.
  82. * @since 0.0.0
  83. * @param s the {@link java.lang.String String} to pad
  84. * @param len the amount of {@code char}s to pad with
  85. * @param pad the {@code char} to pad with
  86. * @param side the side to pad from
  87. * @return the padded {@link java.lang.String String}
  88. */
  89. public static String pad(String s, int len, char pad, boolean side) {
  90. return Strings.pad(s, len, Character.toString(pad), side);
  91. }
  92. /**
  93. * Pads a {@link java.lang.String String} from the left with the provided
  94. * {@code char}.
  95. * @since 0.0.0
  96. * @param s the {@link java.lang.String String} to pad
  97. * @param len the amount of {@code char}s to pad with
  98. * @param pad the {@link java.lang.String String} to pad with
  99. * @param side the side to pad from
  100. * @return the padded {@link java.lang.String String}
  101. */
  102. public static String pad(String s, int len, String pad, boolean side) {
  103. StringBuilder padder = new StringBuilder(Math.max(s.length(), len));
  104. padder.append(s);
  105. if (side) {
  106. while (padder.length() < len) {
  107. padder.insert(0, pad);
  108. }
  109. } else {
  110. while (padder.length() < len) {
  111. padder.append(pad);
  112. }
  113. }
  114. return padder.toString();
  115. }
  116. }
Add Comment
Please, Sign In to add comment