-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathAcronym.java
More file actions
37 lines (34 loc) · 964 Bytes
/
Acronym.java
File metadata and controls
37 lines (34 loc) · 964 Bytes
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
import java.util.Locale;
/**
* 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() {
this.phrase = replaceSpecialChars();
this.phrase = cleanupSpaces();
this.phrase = getFirstCharsAfterSpaces();
return this.phrase;
}
private String getFirstCharsAfterSpaces() {
String resultString = "";
String[] words = phrase.split(" ");
for(String word : words) {
resultString += word.substring(0,1).toUpperCase(Locale.ROOT);
}
return resultString;
}
private String replaceSpecialChars() {
return phrase.replaceAll("[^a-zA-Z0-9']", " ");
}
private String cleanupSpaces() {
return phrase.replaceAll("\\s{2,}", " ").trim();
}
}