File: StringServices.java

package info (click to toggle)
herold 8.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,304 kB
  • sloc: java: 40,460; xml: 513; makefile: 37; sh: 29
file content (982 lines) | stat: -rw-r--r-- 22,074 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
/* 
 * Copyright (C) 2001-2013 Michael Fuchs
 *
 * This file is part of herold.
 * 
 * herold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * herold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with herold.  If not, see <http://www.gnu.org/licenses/>.  
 */
package org.dbdoclet.service;

import java.io.File;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dbdoclet.Sfv;

/**
 * Die Klasse <code>StringServices</code> stellt eine Sammlung statischer
 * Methoden zur Bearbeitung von Zeichenketten und/oder deren
 * Internationalisierung zur Verfügung.
 * 
 * @author <a href="mailto:michael.fuchs@unico-group.com">Michael Fuchs</a>
 * 
 */
public class StringServices {

	public static final Log logger = LogFactory.getLog(StringServices.class);

	public static final String ZERO_WIDTH_SPACE = "\u200b";
	public static final String SIX_PER_EM_SPACE = "\u2006";
	public static final String SOFT_HYPHEN = "\u00ad";

	/**
	 * Die Methode <code>replace</code> ersetzt alle Vorkomnisse eines
	 * bestimmten Musters durch eine Zeichenkette.
	 * 
	 * Die orginale Zeichenkette bleibt unverändert.
	 * 
	 * Das Muster kann in der Zeichenkette durch geschweifte Klammern begrenzt
	 * sein, um Anfang und Ende einer Ersetzung zu kennzeichnen.
	 * 
	 * @param str
	 *            Die Zeichenkette
	 * @param pattern
	 *            Das Suchmuster
	 * @param subst
	 *            Die Ersetzung
	 * @return Die bearbeitete Zeichenkette
	 */
	public static String replace(String str, String pattern, String subst) {

		if (str == null) {
			return null;
		}

		if (pattern == null) {
			return str;
		}

		if (subst == null) {
			subst = "";
		}

		if (pattern.equals(subst)) {
			return str;
		}

		int fromIndex = 0;
		int toIndex = 0;

		String pattern2 = "{" + pattern + "}";

		if (str.indexOf(pattern) == -1 && str.indexOf(pattern2) == -1) {
			return str;
		}

		StringBuffer buffer = new StringBuffer();

		while ((toIndex = str.indexOf(pattern2, toIndex)) != -1) {

			buffer.append(str.substring(fromIndex, toIndex));
			buffer.append(subst);

			fromIndex = toIndex + pattern2.length();
			toIndex = fromIndex;

		} // end of while ()

		buffer.append(str.substring(fromIndex));

		fromIndex = 0;
		toIndex = 0;

		str = buffer.toString();

		buffer = new StringBuffer();

		while ((toIndex = str.indexOf(pattern, toIndex)) != -1) {

			buffer.append(str.substring(fromIndex, toIndex));
			buffer.append(subst);

			fromIndex = toIndex + pattern.length();
			toIndex = fromIndex;

		} // end of while ()

		buffer.append(str.substring(fromIndex));

		return buffer.toString();
	}

	/**
	 * Die Methode <code>replace</code> ersetzt alle Vorkomnisse eines
	 * bestimmten Musters durch eine Zeichenkette ohne die Groß/Kleinscrebung zu
	 * beachten.
	 * 
	 * Die orginale Zeichenkette bleibt unverändert.
	 * 
	 * Das Muster kann in der Zeichenkette durch geschweifte Klammern begrenzt
	 * sein, um Anfang und Ende einer Ersetzung zu kennzeichnen.
	 * 
	 * @param str
	 *            Die Zeichenkette
	 * @param pattern
	 *            Das Suchmuster
	 * @param subst
	 *            Die Ersetzung
	 * @return Die bearbeitete Zeichenkette
	 */
	public static String replaceIgnoreCase(String str, String pattern,
			String subst) {

		if (str == null) {
			return null;
		}

		if (pattern == null) {
			return str;
		}

		if (subst == null) {
			subst = "";
		}

		int fromIndex = 0;
		int toIndex = 0;

		String buffer = "";

		String lowerStr = str.toLowerCase();
		String lowerPattern = pattern.toLowerCase();

		String pattern2 = "{" + lowerPattern + "}";

		while ((toIndex = lowerStr.indexOf(pattern2, toIndex)) != -1) {
			buffer += str.substring(fromIndex, toIndex);
			buffer += subst;

			fromIndex = toIndex + pattern2.length();
			toIndex = fromIndex;

		} // end of while ()

		buffer += str.substring(fromIndex);

		fromIndex = 0;
		toIndex = 0;
		str = buffer;
		buffer = "";

		while ((toIndex = lowerStr.indexOf(lowerPattern, toIndex)) != -1) {
			buffer += str.substring(fromIndex, toIndex);
			buffer += subst;

			fromIndex = toIndex + lowerPattern.length();
			toIndex = fromIndex;

		} // end of while ()

		buffer += str.substring(fromIndex);

		return buffer;
	}

	/**
	 * Die Methode <code>createIndent</code> erzeugt eine Einrückung aus
	 * Leerzeichen der Länge <code>len</code>.
	 * 
	 * @param len
	 *            <code>int</code>
	 * @return <code>String</code>
	 */
	public static String createIndent(int len) {

		StringBuffer indent = new StringBuffer();

		for (int i = 0; i < len; i++) {
			indent.append(' ');
		}

		return indent.toString();
	}

	/**
	 * Die Methode <code>capFirstLetter</code> wandelt den ersten Buchstaben
	 * einer Zeichenkette in einen Großbuchstaben um.
	 * 
	 * Die orginale Zeichenkette bleibt unverändert.
	 * 
	 * @param str
	 *            Die Zeichenkette
	 * @return Die bearbeitete Zeichenkette
	 */
	public static String capFirstLetter(String str) {

		if (str == null || str.length() == 0) {
			return str;
		}

		char[] chars = str.toCharArray();
		chars[0] = Character.toUpperCase(chars[0]);

		return new String(chars);
	}

	/**
	 * Liefert den ersten Buchstaben einer Zeichenkette.
	 */
	public static String getFirstLetter(String str) {

		if (str == null || str.length() == 0) {
			return str;
		}

		char fc = str.toCharArray()[0];
		char[] chars = new char[1];
		chars[0] = fc;

		return new String(chars);
	}

	/**
	 * Die Methode <code>lowerFirstLetter</code> wandelt den ersten Buchstaben
	 * einer Zeichenkette in einen Kleinbuchstaben um.
	 * 
	 * Die orginale Zeichenkette bleibt unverändert.
	 * 
	 * @param str
	 *            Die Zeichenkette
	 * @return Die bearbeitete Zeichenkette
	 */
	public static String lowerFirstLetter(String str) {

		if (str == null || str.length() == 0) {
			return str;
		}

		char[] chars = str.toCharArray();
		chars[0] = Character.toLowerCase(chars[0]);

		return new String(chars);
	}

	/**
	 * Die Methode <code>createJavaIdentifier</code> erzeugt aus einer
	 * Zeichenkette einen gültiges Java Schlüsselwort.
	 * 
	 * Falls der Parameter <code>mangleUnderscores</code> auf wahr gesetzt ist,
	 * werden auch Unterstriche entfernt. Dies ist notwendig, um
	 * <code>jspc.sh</code> konforme Klassennamen zu erzeugen.
	 * <code>jspc.sh</code> ist der JSP-Compiler aus der Tomcat-Distribution.
	 * 
	 * @param str
	 *            Die Zeichenkette
	 * @param mangleUnderscores
	 *            <code>boolean</code>
	 * @return Die bearbeitete Zeichenkette
	 */
	public static String createJavaIdentifier(String str,
			boolean mangleUnderscores) {

		if (str == null || str.length() == 0) {
			return str;
		}

		StringBuffer identifier = new StringBuffer();

		if (Character.isJavaIdentifierStart(str.charAt(0)) == false) {
			identifier.append("i");
		}

		for (int i = 0; i < str.length(); i++) {

			if (Character.isJavaIdentifierPart(str.charAt(i)) == false) {

				if (mangleUnderscores == true) {
					identifier.append(mangleChar(str.charAt(i), "0x"));
				} else {
					identifier.append(mangleChar(str.charAt(i), "_"));
				}

			} else {

				char c = str.charAt(i);

				if (c == '_' && mangleUnderscores == true) {
					identifier.append(mangleChar(str.charAt(i), "0x"));
				} else {
					identifier.append(str.charAt(i));
				}

			}

		} // end of for ()

		return identifier.toString();
	}

	/**
	 * Die Methode <code>createJavaIdentifier</code> erzeugt aus einer
	 * Zeichenkette einen gültiges Java Schlüsselwort.
	 * 
	 * @param str
	 *            Die Zeichenkette
	 * @return Die bearbeitete Zeichenkette
	 */
	public static String createJavaIdentifier(String str) {
		return createJavaIdentifier(str, false);
	}

	/**
	 * Die Methode <code>mangleChar</code> übersetzt ein Schriftzeichen in eine
	 * numerische Darstellung.
	 * 
	 * Die resultierende Zeichenkette startet mit einem Vorspann dem ein 5
	 * Spalten breiter, hexadezimaler Wert folgt. starts with a prefix, the
	 * 
	 * Beispiel: <code>_0002f</code>
	 * 
	 * @param c
	 *            a <code>char</code> value
	 * @param prefix
	 *            a <code>String</code> value
	 * @return <code>String</code>
	 */
	public static String mangleChar(char c, String prefix) {

		String s = Integer.toHexString(c);

		int nzeros = 5 - s.length();

		char[] result = new char[5];

		for (int i = 0; i < nzeros; i++) {
			result[i] = '0';
		}

		for (int i = nzeros, j = 0; i < 5; i++, j++) {
			result[i] = s.charAt(j);
		}

		return (prefix + new String(result));
	}

	/**
	 * Die Methode <code>createHeadline</code> erzeugt eine Überschrift der Form
	 * <code>**** title ****</code>.
	 * 
	 * @param title
	 *            <code>String</code>
	 * @return <code>String</code>
	 */
	public static String createHeadline(String title) {

		if (title == null) {
			throw new IllegalArgumentException(
					"The argument title may not be null!");
		}

		StringBuffer sep = new StringBuffer();

		for (int i = 0; i < title.length() + 4; i++) {
			sep.append('*');
		}

		String buffer = sep + "\n" + "* " + title + " *\n" + sep + "\n\n";

		return buffer;
	}

	/**
	 * Die Methode <code>info</code> erzeugt eine Informationszeile der Form
	 * <code>info.... ..: </code>.
	 * 
	 * Die Länge der erzeugten Zeichenkette beträgt 50 Zeichen.
	 * 
	 * @param line
	 *            <code>String</code>
	 * @return <code>String</code>
	 */
	public static String info(String line) {
		return align(line, 50, '.') + ": ";
	}

	/**
	 * Die Methode <code>info</code> erzeugt eine Informationszeile der Form
	 * <code>info.... ..</code>.
	 * 
	 * Die Länge der erzeugten Zeichenkette beträgt 50 Zeichen.
	 * 
	 * @param line
	 *            <code>String</code>
	 * @return <code>String</code>
	 */
	public static String align(String line) {
		return align(line, 50, '.');
	}

	/**
	 * Die Methode <code>info</code> erzeugt eine Informationszeile der Form
	 * <code>info.... ..</code>.
	 * 
	 * Die Länge der erzeugten Zeichenkette beträgt <code>width</code> Zeichen.
	 * 
	 * @param line
	 *            <code>String</code>
	 * @param width
	 *            <code>int</code>
	 * @return <code>String</code>
	 */
	public static String align(String line, int width) {
		return align(line, width, '.');
	}

	/**
	 * Die Methode <code>info</code> erzeugt eine Informationszeile der Form
	 * <code>info... ..</code>.
	 * 
	 * Die Länge der erzeugten Zeichenkette beträgt <code>width</code> Zeichen.
	 * Das verwendete Trennzeichen kann mit Hilfe des Parameters
	 * <code>fill</code> angegeben werden.
	 * 
	 * @param line
	 *            <code>String</code>
	 * @param width
	 *            <code>int</code>
	 * @param fill
	 *            <code>char</code>
	 * @return <code>String</code>
	 */
	public static String align(String line, int width, char fill) {

		if (line == null || line.length() == 0) {
			throw new IllegalArgumentException(
					"The argument line may not be null!");
		}

		StringBuffer buffer = new StringBuffer(line);

		if (line.length() >= width) {

			buffer.append(fill);
			buffer.append(fill);
			buffer.append(fill);

			return buffer.toString();
		}

		for (int i = line.length(); i < width; i++) {
			buffer.append(fill);
		}

		return buffer.toString();
	}

	/**
	 * The method <code>align</code> returns a string prefix for the output of a
	 * double. If the double is greater than 100 the method returns an empty
	 * string. If the double is between 100 and 10 the method returns one space,
	 * and if the double is less than 10 it returns two spaces.
	 * 
	 * @param d
	 *            a <code>double</code> value
	 * @return <code>String</code>
	 * */
	public static String align(double d) {
		return align((int) d);
	}

	/**
	 * The method <code>align</code> returns a string prefix for the output of
	 * an integer. If the integer is greater than 100 the method returns an
	 * empty string. If the integer is between 100 and 10 the method returns one
	 * space, and if the integer is less than 10 it returns two spaces.
	 * 
	 * @param i
	 *            an <code>int</code> value
	 * @return <code>String</code>
	 * */
	public static String align(int i) {

		if (i >= 1000) {
			return "";
		} // end of if ()

		if (i >= 100) {
			return " ";
		} // end of if ()

		if (i >= 10) {
			return "  ";
		} // end of if ()

		return "   ";
	}

	/**
	 * Die Methode <code>chop</code> bzw. <code>cutSuffix</code> schneidet die
	 * mit <code>suffix</code> angegebene Zeichenkette vom Ende der Zeichenkette
	 * <code>suffix</code> ab.
	 * 
	 * @param text
	 *            <code>String</code>
	 * @param suffix
	 *            <code>String</code>
	 * @return <code>String</code>
	 */
	public static String cutSuffix(String text, String suffix) {

		if (suffix == null) {
			logger.warn(String
					.format("Can't cut off a null suffix (%s)!", text));
			return text;
		}
		if (text == null) {
			return null;
		}

		if (text.length() == 0) {
			return text;
		}

		int index;

		if (text.endsWith(suffix)) {

			index = text.lastIndexOf(suffix);

			if (index != -1) {
				text = text.substring(0, index);
			}
		}

		return text;
	}

	/**
	 * Ein Alias für <code>cutSuffix</code>.
	 */
	public static String chop(String text, String suffix) {
		return cutSuffix(text, suffix);
	}

	/**
	 * Löscht den Buchstaben <code>c</code> vom Anfang und Ende der Zeichenkette
	 * <code>text</code>.
	 */
	public static String trim(String text, char c) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(c);

		return trim(text, buffer.toString());
	}

	/**
	 * Löscht das Muster <code>pattern</code> vom Anfang und Ende der
	 * Zeichenkette <code>text</code>.
	 */
	public static String trim(String text, String pattern) {

		if (text == null) {
			return null;
		}

		if (text.length() == 0) {
			return text;
		}

		while (text.startsWith(pattern)) {
			text = cutPrefix(text, pattern);
		}

		while (text.endsWith(pattern)) {
			text = cutSuffix(text, pattern);
		}

		return text;
	}

	/**
	 * Die Methode <code>cut</code> schneidet die erste mit <code>pattern</code>
	 * angegebene Zeichenkette aus der Zeichenkette <code>text</code> aus.
	 * 
	 * @param text
	 *            <code>String</code>
	 * @param pattern
	 *            <code>String</code>
	 * @return <code>String</code>
	 */
	public static String cut(String text, String pattern) {

		if (text == null) {
			throw new IllegalArgumentException("Parameter text is null!");
		}

		if (pattern == null) {
			throw new IllegalArgumentException("Parameter pattern is null!");
		}

		String buffer;
		int start;
		int end;

		buffer = text;

		start = text.indexOf(pattern);

		if (start != -1) {

			buffer = text.substring(0, start);

			end = start + pattern.length();

			if (end < text.length()) {
				buffer += text.substring(end, text.length());
			}
		}

		return buffer;
	}

	/**
	 * Die Methode <code>cutPrefix</code> entfernt die Zeichenkette
	 * <code>prefix</code> vom Anfang der Zeichenkette <code>text</code>.
	 * 
	 * @param text
	 *            <code>String</code>
	 * @param prefix
	 *            <code>String</code>
	 * @return <code>String</code>
	 */
	public static String cutPrefix(String text, String prefix) {

		if (text == null) {
			throw new IllegalArgumentException("Parameter text is null!");
		}

		if (prefix == null) {
			throw new IllegalArgumentException("Parameter prefix is null!");
		}

		if (text.length() < prefix.length()) {
			return text;
		}

		if (text.equals(prefix)) {
			return "";
		}

		String buffer = text;

		if (text.startsWith(prefix)) {

			if (text.length() == prefix.length()) {
				buffer = "";
			} else {
				buffer = text.substring(prefix.length());
			}
		}

		return buffer;
	}

	/**
	 * Kürzt die Zeichenkette <code>text</code> auf die maximale Länge von
	 * <code>cols</code>.
	 * 
	 * Ist die Zeichenkette <code>text</code> länger als <code>cols</code>
	 * werden die letzten 3 Zeichen durch Punkte ersetzt.
	 */
	public static String shorten(String text, int cols) {

		if (text == null || text.length() == 0) {
			return "";
		}

		if (cols < 5) {
			return text;
		}

		if (text.length() <= cols) {
			return text;
		}

		String msg = new String(text);
		msg = text.substring(0, (cols / 2) - 3);
		msg += "...";
		msg += text.substring(text.length() - (cols / 2));

		return msg;
	}

	public static String splitAt(String text) {
		return splitAt(text, File.separator, 71, Sfv.LSEP);
	}

	/**
	 * Fügt das Trennzeichen <code>Sfv.LSEP</code> an Positionen des Muster
	 * <code>breakable</code> ein, falls die Position innerhalb der Teilkette
	 * größer als 71 ist.
	 */
	public static String splitAt(String text, String breakable) {
		return splitAt(text, breakable, 71, Sfv.LSEP);
	}

	/**
	 * Fügt in eine Zeichenkette Trennzeichen ein.
	 * 
	 * Die Zeichenkette <code>text</code> daraufhin untersucht ob an bestimmten
	 * Stellen Trennzeichen eingefügt werden können. Der Parameter
	 * <code>breakPos</code> bestimmt die minimale Länge einer Teilkette bevor
	 * deas nächste Trennzeichen eingfügt werden kann. Der Parameter
	 * <code>breakable</code> definiert das Muster an dem das Einfügen von
	 * Trennzeichen möglich ist.
	 */
	public static String splitAt(String text, String breakable, int breakPos,
			String splitter) {

		if (text == null || text.length() == 0) {
			return "";
		}

		if (text.length() <= 3) {
			return text;
		}

		if (breakable == null || breakable.length() == 0) {
			return text;
		}

		if (breakPos <= 0 || breakPos >= text.length()) {
			return text;
		}

		if (splitter == null || splitter.length() == 0) {
			return text;
		}

		boolean doSplit = true;
		StringBuffer buffer = new StringBuffer();

		while (doSplit == true) {

			int lessIndex = 0;
			int greaterIndex = 0;

			int index = text.indexOf(breakable);

			while (index != -1) {

				if (index <= breakPos) {
					lessIndex = index;
				}

				if (index >= breakPos && greaterIndex == 0) {
					greaterIndex = index;
				}

				index = text.indexOf(breakable, index + 1);
			}

			if (greaterIndex == 0) {
				greaterIndex = text.length();
			}

			if (lessIndex == 0) {
				lessIndex = breakPos;
			}

			int lessDistance = breakPos - lessIndex;
			int greaterDistance = greaterIndex - breakPos;

			// StringBuffer buffer = new StringBuffer(text);

			if (lessDistance <= greaterDistance
					|| greaterIndex >= text.length()) {

				buffer.append(text.substring(0, lessIndex));
				buffer.append(splitter);

				text = text.substring(lessIndex);
			}

			if (greaterDistance < lessDistance && greaterIndex < text.length()) {

				buffer.append(text.substring(0, greaterIndex));
				buffer.append(splitter);

				text = text.substring(greaterIndex);
			}

			if (text.length() <= breakPos) {

				buffer.append(text);
				doSplit = false;
			}

			/*
			 * logger.debug("text=" + text); logger.debug("text.length=" +
			 * text.length()); logger.debug("breakPos " + breakPos);
			 * logger.debug("lessIndex " + lessIndex);
			 * logger.debug("greaterIndex " + greaterIndex);
			 * logger.debug("lessDistance " + lessDistance);
			 * logger.debug("greaterDistance " + greaterDistance);
			 */
		}

		return buffer.toString();
	}

	public static String fillInt(int num, int width) {

		if (num < 1) {
			num *= -1;
		}

		String buffer = String.valueOf(num);

		while (width > buffer.length()) {
			buffer = "0" + buffer;
		}

		return buffer;
	}

	public static String fillLeadingZero(String num, int width) {

		String buffer = String.valueOf(num);
		boolean negative = false;

		if (buffer.startsWith("-")) {

			buffer = buffer.substring(1);
			negative = true;
		}

		while (width > buffer.length()) {
			buffer = "0" + buffer;
		}

		if (negative == true) {
			buffer = "-" + buffer;
		}

		return buffer;
	}

	public static String makeWrapable(String str) {

		if (str == null || str.length() == 0) {
			return str;
		}

		String[] tokens = { "/", "\\", "-" };

		String wrapable = new String(str);

		for (String token : tokens) {

			if (token == null || token.length() == 0) {
				continue;
			}

			// zero width space
			wrapable = StringServices.replace(wrapable, token, token
					+ ZERO_WIDTH_SPACE);
		}

		return wrapable;
	}

	public static String arrayToString(String[] array) {

		return arrayToString(array, " ");
	}

	public static String arrayToString(String[] array, String sep) {

		if (array == null || array.length == 0) {
			return "";
		}

		StringBuffer buffer = new StringBuffer();

		for (int i = 0; i < array.length; i++) {

			buffer.append(array[i]);
			buffer.append(sep);
		}

		return buffer.toString();
	}

	public static String listToString(List<? extends Object> list) {

		return listToString(list, Sfv.LSEP);
	}

	public static String listToString(List<? extends Object> list, String sep) {

		if (list == null || list.size() == 0) {
			return "";
		}

		StringBuffer buffer = new StringBuffer();

		for (int i = 0; i < list.size(); i++) {

			buffer.append(list.get(i));
			buffer.append(sep);
		}

		return buffer.toString();
	}

	/**
	 * Erstellt einen Rahmen aus Linienzeichen um den angegebenen Text.
	 * 
	 * @param text
	 * @return
	 */
	public static String createBox(String text) {

		StringBuilder buffer = new StringBuilder();

		buffer.append('\u2554');

		for (int i = 0; i < text.length() + 2; i++) {
			buffer.append('\u2550');
		}

		buffer.append('\u2557');
		buffer.append('\n');
		buffer.append("\u2551 ");
		buffer.append(text);
		buffer.append(" \u2551\n");

		buffer.append('\u255A');

		for (int i = 0; i < text.length() + 2; i++) {
			buffer.append('\u2550');
		}

		buffer.append('\u255D');
		buffer.append('\n');

		return buffer.toString();
	}
}