-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathAcronym.java
More file actions
29 lines (26 loc) · 801 Bytes
/
Acronym.java
File metadata and controls
29 lines (26 loc) · 801 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
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 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 {
String text;
Acronym(String phrase) {
text = phrase;
}
String get() {
String result = String.valueOf(text.charAt(0));
String regexStr ="(?<=\\s)(?!_)(?!-).|(?<=-)(?!\\s).|(?<=_)(?!\\s)."; //(?<=\s)(?!_).|(?<=-).|(?<=_)(?!\s).
Pattern pattern = Pattern.compile(regexStr);
Matcher matcher = pattern.matcher(text);
while (matcher.find()){
result = result + matcher.group();
}
return result = result.toUpperCase();
}
}