-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringHelper.java
74 lines (67 loc) · 2.82 KB
/
StringHelper.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
import java.util.Random;
import java.io.File;
///////////////////
// StringHelper: contains many helper fuctions for strings used throughout portions of the program
///////////////////
public class StringHelper extends OS {
//splitOnDelimeter - split the input string into an array based on the delimiter
public String[] splitOnDelimeter(String inputString, String delimiter) {
String[] data = inputString.split(delimiter);
return data;
}
//substringIsInString - check to see if the given sub string exists within the input string
public boolean substringIsInString(String inputString, String subString) {
if (inputString.toLowerCase().contains(subString.toLowerCase())) {
return true;
}
return false;
}
//findTokenIndexInArray - loops through the input array to see if the token to find exists within it
public int findTokenIndexInArray(String[] inputArray, String tokenToFind) {
for (int i = 0; i < inputArray.length; i++) {
if (inputArray[i].contains("|")) {
String[] splitSubArray = splitOnDelimeter(inputArray[i], "\\|");
for (int j = 0; j < splitSubArray.length; j++) {
if (splitSubArray[j].equals(tokenToFind)) {
return i;
}
}
} else {
if (inputArray[i].equals(tokenToFind)) {
return i;
}
}
}
return -1;
}
public String returnOrdinal(int inputNum) {
String[] sufixes = new String[] { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" };
switch (inputNum % 100) {
case 11:
case 12:
case 13:
return inputNum + "th";
default:
return inputNum + sufixes[inputNum % 10];
}
}
//removeBadWhitespace - removes all white space except for that which descriptor tokens are found
public String removeBadWhitespace(String inputString, String[] validKeys) {
//first remove all white space from the line
inputString = inputString.replaceAll("[\\s]*","");
//loop through all valid keys
for (int i = 0; i < validKeys.length; i++) {
//if the valid key even contains a space
if (substringIsInString(validKeys[i], " ")) {
String keyWithSpace = validKeys[i];
String keyWithoutSpace = validKeys[i].replaceAll(" ", "");
//if the key is present in the string without spaces
if (substringIsInString(inputString, keyWithoutSpace)) {
//replace with spaces
inputString = inputString.replaceAll(keyWithoutSpace, keyWithSpace);
}
}
}
return inputString;
}
}