1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 
7 package de.dlr.ts.commons.tools;
8 
9 import java.util.Arrays;
10 import java.util.List;
11 
12 /**
13  *
14  * @author <a href="mailto:maximiliano.bottazzi@dlr.de">Maximiliano Bottazzi</a>
15  */
16 public class StringTools
17 {
18     /**
19      * Repeats <code>charToRepeat</code> <code>times</code> times.
20      *
21      * @param charToRepeat
22      * @param times
23      * @return
24      */
repeatChar(final String charToRepeat, int times)25     public static String repeatChar(final String charToRepeat, int times)
26     {
27         String ret = "";
28         for (int i = 0; i < times; i++)
29             ret += charToRepeat;
30 
31         return ret;
32     }
33 
34     /**
35      *
36      * @param text
37      * @param width
38      * @return
39      */
centerText(final String text, int width)40     public static String centerText(final String text, int width)
41     {
42         String ret;
43         int head = (width - text.length()) / 2;
44 
45         ret = repeatChar(" ", head);
46         ret += text;
47         ret += repeatChar(" ", width - (head + text.length()));
48 
49         return ret;
50     }
51 
52     /**
53      *
54      * For example: <code>centerText("Text", 20, "**")</code> returns <pre><b>**     text    **</b></pre>.
55      *
56      * @param text
57      * @param width
58      * @param borders
59      * @return
60      */
centerText(String text, int width, String borders)61     public static String centerText(String text, int width, String borders)
62     {
63         return borders + StringTools.centerText(text, width - (borders.length()*2)) + borders;
64     }
65 
66     /**
67      * For example: <code>alignLeft("Text", 20, "#")</code> returns <pre><b># Text             #</b></pre>
68      *
69      * @param text Text to align left.
70      * @param width Total width of the returned {@link String}.
71      * @param borders Leading and trailing char of the returned {@link String}.
72      * @return a {@link String} of length <code>witdh</code>,
73      * begining and ending with <code>borders</code> and with <code>text</code> in the middle.
74      */
alignLeft(String text, int width, String borders)75     public static String alignLeft(String text, int width, String borders)
76     {
77         int space = width - (borders.length() + 1 + text.length() + borders.length());
78         return borders + " " + text + repeatChar(" ", space) + borders;
79     }
80 
alignLeft(String text, int width)81     public static String alignLeft(String text, int width)
82     {
83         int space = width - (1 + text.length());
84         return " " + text + repeatChar(" ", space);
85     }
86 
alignRight(String text, int width, String borders)87     public static String alignRight(String text, int width, String borders)
88     {
89         int space = width - (borders.length() + 1 + text.length() + borders.length());
90         return borders + " " + repeatChar(" ", space) + text + borders;
91     }
92 
93     /**
94      *
95      * @param text
96      * @param width
97      * @param direction L = left, R = right, C = center
98      * @return
99      */
align(String text, int width, String direction)100     public static String align(String text, int width, String direction)
101     {
102         if(direction.equalsIgnoreCase("L"))
103             return alignLeft(text, width);
104         if(direction.equalsIgnoreCase("R"))
105             return alignRight(text, width);
106 
107         return centerText(text, width);
108     }
109 
align(int text, int width, String direction)110     public static String align(int text, int width, String direction) {
111         return align("" + text, width, direction);
112     }
113 
alignRight(String text, int width)114     public static String alignRight(String text, int width)
115     {
116         int space = width - (1 + text.length());
117         return " " + repeatChar(" ", space) + text;
118     }
119 
120     /**
121      * Centers a text and adds leading and trailing characters with the
122      * <code>charToFillWith</code> character and <code>length</code> times.
123      * For example: <code>centerAndFillWithChar("Text", '#', 20)</code> returns
124      * <b>####### Text #######</b>
125      *
126      * @param text Text to center
127      * @param charToFillWith Character for filling
128      * @param length
129      * @return
130      */
centerAndFillWithChar(final String text, char charToFillWith, int length)131     public static String centerAndFillWithChar(final String text, char charToFillWith, int length)
132     {
133         if(text.length() + 2 > length)
134             return text;
135 
136         String tmp = " " + text + " ";
137         int rest = length - (text.length() + 2);
138 
139         for (int i = 0; i < rest / 2; i++)
140             tmp = charToFillWith + tmp;
141 
142         for (int i = 0; i < (rest / 2) + rest%2; i++)
143             tmp = tmp + charToFillWith;
144 
145         return tmp;
146     }
147 
148     /**
149      * Aligns a text right and adds trailing characters with the
150      * <code>charToFillWith</code> character and <code>length</code> times.
151      * For example: <code>centerAndFillWithChar("Text", '#', 20)</code> returns
152      * <b>Text ###############</b>
153      *
154      *
155      * @param text
156      * @param charToFillWith
157      * @param length
158      * @return
159      */
alignLeftAndFillWithChar(final String text, char charToFillWith, int length)160     public static String alignLeftAndFillWithChar(final String text, char charToFillWith, int length)
161     {
162         if(text.length() + 1 > length)
163             return text;
164 
165         String tmp = text + " ";
166 
167         for (int i = 0; i < length - (text.length() + 1); i++)
168             tmp = tmp + charToFillWith;
169 
170         return tmp;
171     }
172 
173     /**
174      * Aligns a text right and adds leading characters with the
175      * <code>charToFillWith</code> character and <code>length</code> times.
176      * For example: <code>centerAndFillWithChar("Text", '#', 20)</code> returns
177      * <b>############### Text</b>
178      *
179      * @param text
180      * @param charToFillWith
181      * @param length
182      * @return
183      */
alignRightAndFillWithChar(final String text, char charToFillWith, int length)184     public static String alignRightAndFillWithChar(final String text, char charToFillWith, int length)
185     {
186          if(text.length() + 1 > length)
187             return text;
188 
189         String tmp = " " + text;
190 
191         for (int i = 0; i < length - (text.length() + 1); i++)
192             tmp = charToFillWith + tmp;
193 
194         return tmp;
195     }
196 
197     /**
198      *
199      * @param originalString
200      * @param chars
201      * @param i
202      * @return
203      */
204     @Deprecated
fillStringWithChars(String originalString, String chars, int i)205     public static String fillStringWithChars(String originalString, String chars, int i)
206     {
207         String ret = "";
208 
209         for (int j = 0; j < i - originalString.length(); j++)
210             ret += chars;
211 
212         return ret;
213     }
214 
215 
216 
217     /**
218      *
219      * @param string
220      * @return
221      */
convertStringToList(String string)222     public static List<String> convertStringToList(String string)
223     {
224         return Arrays.asList(string.split(System.lineSeparator()));
225     }
226 
227 }
228