1. Introduction
1.导言
When handling names, it can be beneficial to shorten them into abbreviated strings using each word’s first character. In this tutorial, let’s look at different ways to implement this functionality in Java.
在处理名称时,使用每个单词的第一个字符将其缩短为缩写字符串可能是有益的。在本教程中,我们将了解在 Java 中实现这一功能的不同方法。
2. Assumptions
2.假设
When creating abbreviations, we only consider words that begin with the alphabet. Any other words are excluded from the process. Additionally, the abbreviation may result in an empty string with no valid words. Furthermore, we convert the entire string to uppercase.
创建缩写时,我们只考虑以字母开头的单词。任何其他单词都将被排除在外。此外,缩写可能会产生一个没有有效单词的空字符串。此外,我们还会将整个字符串转换为大写字母。
3. Using Loops
3.使用循环
We can split the text by spaces and use a for loop to iterate over each word. Subsequently, we can take the first character of each valid word and build the initials:
我们可以用空格分割文本,并使用 for 循环遍历每个单词。随后,我们可以取每个有效单词的第一个字符,并建立首字母缩写:
String getInitialUsingLoop(String name) {
if (name == null || name.isEmpty()) {
return "";
}
String[] parts = name.split("\\s+");
StringBuilder initials = new StringBuilder();
for (String part : parts) {
if (part.matches("[a-zA-Z].*")) {
initials.append(part.charAt(0));
}
}
return initials.toString().toUpperCase();
}
In the above code, we check if a word starts with the alphabet using a regex and then extract the first character to form the abbreviation.
在上述代码中,我们使用 regex 检查单词是否以字母开头,然后提取第一个字符组成缩写。
We can write a unit test to check different cases using JUnit:
我们可以使用 JUnit 编写单元测试来检查不同的情况:
@ParameterizedTest
@CsvSource({"John F Kennedy,JFK", ",''", "'',''", "Not Correct 88text,NC", "michael jackson,MJ", "123,''", "123 234A,''", "1test 2test, ''"})
void getInitialFromName_usingLoop(String input, String expected) {
String initial = getInitialUsingLoop(input);
assertEquals(expected, initial);
}
In the above test case, we utilized JUnit’s parameterized test feature to specify multiple input and expected output combinations. As a result, we can ensure comprehensive coverage and validation of the functionality under different conditions.
在上述测试用例中,我们利用 JUnit 的参数化测试功能指定了多个输入和预期输出组合。因此,我们可以确保在不同条件下对功能进行全面覆盖和验证。
4. Using StringTokenizer
4.使用 StringTokenizer
We can use the StringTokenizer to split the text into words. Let’s look at the implementation:
我们可以使用 StringTokenizer 将文本分割成单词。让我们来看看实现方法:
String getInitialUsingStringTokenizer(String name)
if (name == null || name.isEmpty()) {
return "";
}
StringTokenizer tokenizer = new StringTokenizer(name);
StringBuilder initials = new StringBuilder();
while (tokenizer.hasMoreTokens()) {
String part = tokenizer.nextToken();
if (part.matches("[a-zA-Z].*")) {
initials.append(part.charAt(0));
}
}
return initials.toString().toUpperCase();
}
This code is similar to the previous implementation, except we use StringTokenizer instead of the split() method.
这段代码与之前的实现类似,只是我们使用了 StringTokenizer 而不是 split() 方法。
We can utilize the same parameterized test as before for this method.
对于这种方法,我们可以使用与之前相同的参数化测试。
5. Using Regular Expressions
5.使用正则表达式
Another way to implement this functionality is by using regular expressions. We can capture the first character of each valid word by using a Regex Capture:
实现此功能的另一种方法是使用正则表达式。我们可以使用 Regex Capture 来捕获每个有效单词的第一个字符:
String getInitialUsingRegex(String name) {
if (name == null || name.isEmpty()) {
return "";
}
Pattern pattern = Pattern.compile("\\b[a-zA-Z]");
Matcher matcher = pattern.matcher(name);
StringBuilder initials = new StringBuilder();
while (matcher.find()) {
initials.append(matcher.group());
}
return initials.toString().toUpperCase();
}
Similarly, we can create a test case to validate the implementation.
同样,我们也可以创建一个测试用例来验证实现。
6. Using the Stream API
6.使用流应用程序接口
We can also use the functional programming-based Stream API, which has been available since Java 8. Now, let’s delve into the implementation:
我们还可以使用基于函数式编程的流 API,该 API 自 Java 8 开始提供。现在,让我们深入了解其实现:
String getInitialUsingStreamsAPI(String name) {
if (name == null || name.isEmpty()) {
return "";
}
return Arrays.stream(name.split("\\s+"))
.filter(part -> part.matches("[a-zA-Z].*"))
.map(part -> part.substring(0, 1))
.collect(Collectors.joining())
.toUpperCase();
}
In this scenario, we combined the filter(), map(), and collect() methods to accomplish the goal. We can use a similar parameterized test to verify this implementation as well.
在这种情况下,我们结合了 filter()、map() 和 collect() 方法来实现目标。我们也可以使用类似的参数化测试来验证这一实现。
7. Conclusion
7.结论
This article discussed various methods for extracting the initials from a name in Java. These methods can also generate acronyms for any text, not just names. Furthermore, we explored the traditional loop-based approaches, regular expression, and more functional programming approaches to achieve the same outcome. Depending on the specific scenario, developers can choose the approach that best suits their needs.
本文讨论了在 Java 中提取姓名首字母的各种方法。这些方法还可以为任何文本生成首字母缩写,而不仅仅是名称。此外,我们还探讨了传统的基于循环的方法、正则表达式和更多的函数式编程方法来实现相同的结果。根据具体场景,开发人员可以选择最适合自己需要的方法。
As always, the sample code used in this tutorial is available over on GitHub.
与往常一样,本教程中使用的示例代码可在 GitHub 上获取。