Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

StringUtils.java 2.3KB

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