You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringUtils.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.afp.util;
  19. /**
  20. * Library of utility methods useful in dealing with strings.
  21. *
  22. */
  23. public final class StringUtils {
  24. private StringUtils() {
  25. }
  26. /**
  27. * Padds the string to the left with the given character for
  28. * the specified length.
  29. * @param input The input string.
  30. * @param padding The char used for padding.
  31. * @param length The length of the new string.
  32. * @return The padded string.
  33. */
  34. public static String lpad(String input, char padding, int length) {
  35. if (input == null) {
  36. input = new String();
  37. }
  38. if (input.length() >= length) {
  39. return input;
  40. } else {
  41. StringBuffer result = new StringBuffer();
  42. int numChars = length - input.length();
  43. for (int i = 0; i < numChars; i++) {
  44. result.append(padding);
  45. }
  46. result.append(input);
  47. return result.toString();
  48. }
  49. }
  50. /**
  51. * Padds the string to the right with the given character for
  52. * the specified length.
  53. * @param input The input string.
  54. * @param padding The char used for padding.
  55. * @param length The length of the new string.
  56. * @return The padded string.
  57. */
  58. public static String rpad(String input, char padding, int length) {
  59. if (input == null) {
  60. input = new String();
  61. }
  62. if (input.length() >= length) {
  63. return input;
  64. } else {
  65. StringBuffer result = new StringBuffer(input);
  66. int numChars = length - input.length();
  67. for (int i = 0; i < numChars; i++) {
  68. result.append(padding);
  69. }
  70. return result.toString();
  71. }
  72. }
  73. }