[英]Breaking apart a String that includes last name, first name, and initial in Java
问题:
在 Java 中拆分包含姓氏、名字和首字母的字符串。
我的代码:
int index = 0;
String first = "", last= "", initial= "";
for ( int i = 0; i < name.length(); i++) {
if (name.charAt(i) == ',') {
last = name.substring(0,i);
index = i+1;
}
else if(name.charAt(i) ==' ') {
first = name.substring(index,i);
}
else if(name.charAt(i) == '.') {
initial = name.substring(i-1, i);
}
}
return (first + " " +initial+ " " + last);
}
我无法弄清楚我做错了什么。 我花了 5 个多小时试图弄清楚并需要一些帮助。
当我运行时,测试似乎失败了。
我在控制台中得到了什么:
stringReformat demo1. the substring(1,5) is: eato
stringReformat demo2. the character in position 4 is NOT a comma, it is: o
Failed testStringReformat: Expected: (Alex P Keaton) Actual ( Alex P Keaton)
stringReformat demo1. the substring(1,5) is: teem
stringReformat demo2. the character in position 4 is NOT a comma, it is: m
Failed testStringReformat: Expected: (Lois Steem) Actual ( Steem)
stringReformat demo1. the substring(1,5) is: oyd,
stringReformat demo2. the character in position 4 is a comma
Failed testStringReformat: Expected: (Luke A Boyd) Actual ( Luke A. A Boyd)
stringReformat demo1. the substring(1,5) is: ssen
stringReformat demo2. the character in position 4 is NOT a comma, it is: n
Failed testStringReformat: Expected: (Lotta B Essen) Actual ( Lotta B Essen)
stringReformat demo1. the substring(1,5) is: itte
stringReformat demo2. the character in position 4 is NOT a comma, it is: e
Failed testStringReformat: Expected: (Candace B Rittenoff) Actual ( Candace B Rittenoff)
stringReformat demo1. the substring(1,5) is: arly
stringReformat demo2. the character in position 4 is NOT a comma, it is: y
Failed testStringReformat: Expected: (Stu Early) Actual ( Early)
stringReformat demo1. the substring(1,5) is: eato
stringReformat demo2. the character in position 4 is NOT a comma, it is: o
Failed testStringReformat: Expected: (Alex P Keaton) Actual ( Alex P Keaton)
stringReformat demo1. the substring(1,5) is: rupt
stringReformat demo2. the character in position 4 is NOT a comma, it is: t
Failed testStringReformat: Expected: (Vera A Bruptly) Actual ( Vera A. A Bruptly)
stringReformat demo1. the substring(1,5) is: atso
stringReformat demo2. the character in position 4 is NOT a comma, it is: o
Failed testStringReformat: Expected: (Wanda Y Datso) Actual ( Wanda Y Datso)
stringReformat demo1. the substring(1,5) is: ilve
stringReformat demo2. the character in position 4 is NOT a comma, it is: e
Failed testStringReformat: Expected: (I O Silver) Actual ( I O Silver)
finished stringReformat tests
Finished tests
您编写的代码不会通过所有测试,并且如果不更改“它的工作方式”就无法修复它。 问题是空格不能用于分割或分隔各个部分。 如果像"Steem, Lois"
这样的字符串不以空格字符结尾,该方法将简单地返回不正确的" Steem"
。 此外,如果字符串在初始点之后以" "
结尾,它将在名字中包含初始值。
我为您提供了两种不同的解决方案。
假设我们有一个格式为"{lastname}, {firstname} {initial}."
其中首字母是可选的。 该字符串被传递给StringFormat
方法,因此我们假设参数格式正确。
第一个解决方案删除空格,如果以点结尾,则提取首字母,然后提取名字和姓氏。 这一切都是通过字符串操作完成的。
public static String StringReformat(String name) {
String initial = null, first, last;
//Removes all whitespaces
name = name.replaceAll("\\s", "");
//Checks if there is initial and extracts it
if(name.endsWith(".")) {
initial = name.substring(name.length() - 2, name.length() - 1);
name = name.substring(0, name.length() - 2);
}
//Gets first and last name
String[] tokens = name.split(",");
first = tokens[1];
last = tokens[0];
if(initial != null) {
return first + " " + initial + " " + last;
} else {
return first + " " + last;
}
}
第二种解决方案使用 Matcher 来检查指定的名称字符串是否与名称正则表达式匹配并提取名称部分。
public static String StringReformat(String name) {
Pattern pattern = Pattern.compile(
"([a-zA-Z]+)\\s*,\\s*([a-zA-Z]+)\\s*(?:([A-Z])\\.)?");
Matcher matcher = pattern.matcher(name.trim());
if(matcher.matches()) {
String last = matcher.group(1);
String first = matcher.group(2);
String initial = matcher.group(3);
if(initial != null) {
return first + " " + initial + " " + last;
} else {
return first + " " + last;
}
}
throw new IllegalArgumentException("Invalid name argument format!");
}
第二种解决方案更好,更“可维护”,但如果您是初学者,则更难理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.