forked from ChestShop-authors/ChestShop-3
-
Notifications
You must be signed in to change notification settings - Fork 2
/
StringUtil.java
117 lines (106 loc) · 3.16 KB
/
StringUtil.java
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
package com.Acrobot.Breeze.Utils;
import com.google.common.base.Joiner;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.ChatColor;
/**
* @author Acrobot
*/
public class StringUtil {
/**
* Capitalizes every first letter of a word
*
* @param string
* String to reformat
* @param separator
* Word separator
* @return Reformatted string
*/
public static String capitalizeFirstLetter(String string, char separator) {
if (string == null || string.length() == 0) {
return string;
}
int length = string.length();
StringBuilder builder = new StringBuilder(length);
boolean capitalizeNext = true;
for (int i = 0; i < length; i++) {
char ch = string.charAt(i);
if (ch == separator) {
builder.append(' ');
capitalizeNext = true;
} else if (capitalizeNext) {
builder.append(Character.toTitleCase(ch));
capitalizeNext = false;
} else {
builder.append(Character.toLowerCase(ch));
}
}
return builder.toString();
}
/**
* Removes all whitespace characters from the given string. Also capitalizes every first character of a word.
*
* @param string
* The string to remove whitespace characters from.
* @return The string with whitespace characters removed.
*/
public static String stripWhitespaces(String string) {
String result = StringUtil.capitalizeFirstLetter(string);
return result.replace(" ", "");
}
/**
* Capitalizes every first letter of a word
*
* @param string
* String to reformat
* @return Reformatted string
* @see com.Acrobot.Breeze.Utils.StringUtil#capitalizeFirstLetter(String, char)
*/
public static String capitalizeFirstLetter(String string) {
return capitalizeFirstLetter(string, ' ');
}
/**
* Joins a String array
*
* @param array
* array to join
* @return Joined array
*/
public static String joinArray(String[] array) {
return Joiner.on(' ').join(array);
}
/**
* Joins an iterable
*
* @param array
* Iterable
* @return Joined iterable
*/
public static String joinArray(Iterable<?> array) {
return Joiner.on(' ').join(array);
}
/**
* Strips colour codes from a string
*
* @param string
* String to strip
* @return Stripped string
*/
public static String stripColourCodes(String string) {
return ChatColor.stripColor(string);
}
/**
* Stips colour codes from an array of strings
*
* @param strings
* Strings to strip the codes from
* @return Stripped strings
*/
public static String[] stripColourCodes(String[] strings) {
List<String> output = new ArrayList<String>();
for (String string : strings) {
output.add(stripColourCodes(string));
}
return output.toArray(new String[output.size()]);
}
}