• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 package jme3tools.navigation;
6 
7 import java.util.regex.Pattern;
8 
9 /**
10  * A collection of String utilities.
11  *
12  * @author Benjamin Jakobus
13  * @version 1.0
14  */
15 public class StringUtil {
16 
17     /**
18      * Splits a newline (\n) delimited string into an array of strings
19      *
20      * @param str the string to split up
21      * @param delimiter the delimiter to use in splitting
22      * @returns an array of String objects equivalent to str
23      */
splitDelimitedStr(String str, String delimiter)24     public String[] splitDelimitedStr(String str, String delimiter) {
25         Pattern pttn = Pattern.compile(delimiter);
26         return pttn.split(str);
27     }
28 
29     /**
30      * Right aligns a long number with spaces for printing
31      *
32      * @param num the number to be aligned
33      * @param totalLen the total length of the padded string
34      * @return the padded number
35      */
padNum(long num, int totalLen)36     public String padNum(long num, int totalLen) {
37         String numStr = Long.toString(num);
38         int len = totalLen - numStr.length();
39         String pads = "";
40         for (int i = 0; i < len; i++) {
41             pads += " ";
42         }
43         return pads + numStr;
44     }
45 
46     /**
47      * Right aligns a long number with zeros for printing
48      *
49      * @param num the number to be aligned
50      * @param totalLen the total length of the padded string
51      * @return the padded number
52      */
padNumZero(long num, int totalLen)53     public String padNumZero(long num, int totalLen) {
54         String numStr = Long.toString(num);
55         int len = totalLen - numStr.length();
56         String pads = "";
57         for (int i = 0; i < len; i++) {
58             pads += "0";
59         }
60         return pads + numStr;
61     }
62 
63     /**
64      * Right aligns an integer number with spaces for printing
65      *
66      * @param num the number to be aligned
67      * @param totalLen the total length of the padded string
68      * @return the padded number
69      */
padNum(int num, int totalLen)70     public String padNum(int num, int totalLen) {
71         String numStr = Integer.toString(num);
72         int len = totalLen - numStr.length();
73         String pads = "";
74         for (int i = 0; i < len; i++) {
75             pads += " ";
76         }
77         return pads + numStr;
78     }
79 
80     /**
81      * Right aligns an integer number with zeros for printing
82      *
83      * @param num the number to be aligned
84      * @param totalLen the total length of the padded string
85      * @return the padded number
86      */
padNumZero(int num, int totalLen)87     public String padNumZero(int num, int totalLen) {
88         String numStr = Integer.toString(num);
89         int len = totalLen - numStr.length();
90         String pads = "";
91         for (int i = 0; i < len; i++) {
92             pads += "0";
93         }
94         return pads + numStr;
95     }
96 
97     /**
98      * Right aligns a double number with spaces for printing
99      *
100      * @param num the number to be aligned
101      * @param wholeLen the total length of the padded string
102      * @return the padded number
103      */
padNum(double num, int wholeLen, int decimalPlaces)104     public String padNum(double num, int wholeLen, int decimalPlaces) {
105         String numStr = Double.toString(num);
106         int dpLoc = numStr.indexOf(".");
107 
108         int len = wholeLen - dpLoc;
109         String pads = "";
110         for (int i = 0; i < len; i++) {
111             pads += " ";
112         }
113 
114         numStr = pads + numStr;
115 
116         dpLoc = numStr.indexOf(".");
117 
118         if (dpLoc + 1 + decimalPlaces > numStr.substring(dpLoc).length()) {
119             return numStr;
120         }
121         return numStr.substring(0, dpLoc + 1 + decimalPlaces);
122     }
123 
124     /**
125      * Right aligns a double number with zeros for printing
126      *
127      * @param num the number to be aligned
128      * @param wholeLen the total length of the padded string
129      * @return the padded number
130      */
padNumZero(double num, int wholeLen, int decimalPlaces)131     public String padNumZero(double num, int wholeLen, int decimalPlaces) {
132         String numStr = Double.toString(num);
133         int dpLoc = numStr.indexOf(".");
134 
135         int len = wholeLen - dpLoc;
136         String pads = "";
137         for (int i = 0; i < len; i++) {
138             pads += "0";
139         }
140 
141         numStr = pads + numStr;
142 
143         dpLoc = numStr.indexOf(".");
144 
145         if (dpLoc + 1 + decimalPlaces > numStr.substring(dpLoc).length()) {
146             return numStr;
147         }
148         return numStr.substring(0, dpLoc + 1 + decimalPlaces);
149     }
150 
151     /**
152      * Right aligns a float number with spaces for printing
153      *
154      * @param num the number to be aligned
155      * @param wholeLen the total length of the padded string
156      * @return the padded number
157      */
padNum(float num, int wholeLen, int decimalPlaces)158     public String padNum(float num, int wholeLen, int decimalPlaces) {
159         String numStr = Float.toString(num);
160         int dpLoc = numStr.indexOf(".");
161 
162         int len = wholeLen - dpLoc;
163         String pads = "";
164         for (int i = 0; i < len; i++) {
165             pads += " ";
166         }
167 
168         numStr = pads + numStr;
169 
170         dpLoc = numStr.indexOf(".");
171 
172         if (dpLoc + 1 + decimalPlaces > numStr.substring(dpLoc).length()) {
173             return numStr;
174         }
175         return numStr.substring(0, dpLoc + 1 + decimalPlaces);
176     }
177 
178     /**
179      * Right aligns a float number with zeros for printing
180      *
181      * @param num the number to be aligned
182      * @param wholeLen the total length of the padded string
183      * @return the padded number
184      */
padNumZero(float num, int wholeLen, int decimalPlaces)185     public String padNumZero(float num, int wholeLen, int decimalPlaces) {
186         String numStr = Float.toString(num);
187         int dpLoc = numStr.indexOf(".");
188 
189         int len = wholeLen - dpLoc;
190         String pads = "";
191 
192         if (numStr.charAt(0) == '-') {
193             len += 1;
194             for (int i = 0; i < len; i++) {
195                 pads += "0";
196             }
197             pads = "-" + pads;
198             numStr = pads + numStr.substring(1);
199         } else {
200             for (int i = 0; i < len; i++) {
201                 pads += "0";
202             }
203             numStr = pads + numStr;
204         }
205 
206         dpLoc = numStr.indexOf(".");
207         int length = numStr.substring(dpLoc).length();
208         while (length < decimalPlaces) {
209             numStr += "0";
210         }
211         return numStr;
212 
213     }
214 
215     /**
216      * Right aligns a float number with zeros for printing
217      *
218      * @param num the number to be aligned
219      * @param wholeLen the total length of the padded string
220      * @return the padded number
221      */
padStringRight(String input, int wholeLen)222     public String padStringRight(String input, int wholeLen) {
223         for (int i = input.length(); i < wholeLen; i++) {
224             input += " ";
225         }
226         return input;
227     }
228 
229     /**
230      * @param arr a boolean array to be represented as a string
231      * @return the array as a string
232      */
boolArrToStr(boolean[] arr)233     public String boolArrToStr(boolean[] arr) {
234         String output = "";
235         for (int i = 0; i < arr.length; i++) {
236             if (arr[i]) {
237                 output += "1";
238             } else {
239                 output += "0";
240             }
241         }
242         return output;
243     }
244 
245     /**
246      * Formats a double nicely for printing: THIS DOES NOT ROUND!!!!
247      * @param num the double to be turned into a pretty string
248      * @return the pretty string
249      */
prettyNum(double num)250     public String prettyNum(double num) {
251         String numStr = (new Double(num)).toString();
252 
253         while (numStr.length() < 4) {
254             numStr += "0";
255         }
256 
257         numStr = numStr.substring(0, numStr.indexOf(".") + 3);
258         return numStr;
259     }
260 }
261