-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathAcronym.java
More file actions
56 lines (41 loc) · 1.32 KB
/
Acronym.java
File metadata and controls
56 lines (41 loc) · 1.32 KB
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
/**
* Convert a phrase to its acronym.
*
* Techies love their TLA (Three Letter Acronyms)!
*
* Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).
*/
class Acronym {
private String phrase;
Acronym(String phrase) {
this.phrase = phrase;
}
String get() {
lineDeleter(phrase);
// if(phrase.contains(" - ")) {
// phrase = phrase.replace("-"," ");
// return phrase;
// }
// PhraseContainsLine AND leftAndRightIsWhiteSpace
// phrase = phrase.replace(" - "," ");
String[] words = phrase.split(" ");
StringBuilder acronymPhrase = new StringBuilder();
for (String word : words) {
if (word.startsWith("_") && word.endsWith("_")) {
String acronymMinusUnderline = word.substring(1, word.length() - 1);
acronymPhrase.append(acronymMinusUnderline.substring(0, 1).toUpperCase());
} else if (word.contains("-")) {
word.replace(word, "\\s");
} else {
acronymPhrase.append(word.substring(0, 1).toUpperCase());
}
}
return acronymPhrase.toString();
}
public String lineDeleter(String phrase){
String result = phrase = phrase.replace(" - "," ");
result = phrase.replaceAll("_", "");
result = phrase.replaceAll("-", " ");
return result;
}
}