KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > afp > tools > StringUtils


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
18 /* $Id: StringUtils.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.afp.tools;
21
22 /**
23  * Library of utility methods useful in dealing with strings.
24  *
25  */

26 public class StringUtils {
27
28     /**
29      * Padds the string to the left with the given character for
30      * the specified length.
31      * @param input The input string.
32      * @param padding The char used for padding.
33      * @param length The length of the new string.
34      * @return The padded string.
35      */

36     public static String JavaDoc lpad(String JavaDoc input, char padding, int length) {
37
38         if (input == null) {
39             input = new String JavaDoc();
40         }
41
42         if (input.length() >= length) {
43             return input;
44         } else {
45             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
46             int numChars = length - input.length();
47             for (int i = 0; i < numChars; i++) {
48                 result.append(padding);
49             }
50             result.append(input);
51             return result.toString();
52         }
53     }
54
55     /**
56      * Padds the string to the right with the given character for
57      * the specified length.
58      * @param input The input string.
59      * @param padding The char used for padding.
60      * @param length The length of the new string.
61      * @return The padded string.
62      */

63     public static String JavaDoc rpad(String JavaDoc input, char padding, int length) {
64
65         if (input == null) {
66             input = new String JavaDoc();
67         }
68
69         if (input.length() >= length) {
70             return input;
71         } else {
72             StringBuffer JavaDoc result = new StringBuffer JavaDoc(input);
73             int numChars = length - input.length();
74             for (int i = 0; i < numChars; i++) {
75                 result.append(padding);
76             }
77             return result.toString();
78         }
79     }
80
81 }
Popular Tags