英文:
How to add comma/and between string array in java
问题
以下是翻译好的代码部分:
public class SeprateAuthors {
public static void main(String[] args) {
String[] authors0 = {"a", "b"};
String[] authors1 = {"a", "b", "c"};
String[] authors2 = {"a", "b", "c", "d"};
String[] authors3 = {"a", "b", "c", "d", "e", "f"};
System.out.println(displayAuthors(authors0));
System.out.println(displayAuthors(authors1));
System.out.println(displayAuthors(authors2));
System.out.println(displayAuthors(authors3));
}
public static String displayAuthors(String[] authors) {
StringBuilder stringBuilder = new StringBuilder();
String stringAuthors = "";
String prefixComma = ", ";
String prefixAnd = " and ";
if ((authors != null) && (authors.length > 0)) {
for (int i = 0; i < authors.length; i++) {
if (i < authors.length - 2) {
stringBuilder.append(authors[i]).append(prefixComma);
} else {
stringBuilder.append(authors[i]).append(prefixAnd);
}
}
// Java Remove extra Characters("and ") from String
stringAuthors = stringBuilder.substring(0, stringBuilder.length() - 4);
}
return stringAuthors;
}
}
注意:我已经将代码中的HTML实体字符(如"
)转换为普通的双引号("
)以获得更清晰的代码。
英文:
i get an array of authors from json response and i have to display authors name in TextView(android) in proper format like =>Viraj, Chetan and George R. R. Martin
my code work fine, but it's a mess....
public class SeprateAuthors {
public static void main(String[] args) {
String[] authors0 = {"a", "b"};
String[] authors1 = {"a", "b", "c"};
String[] authors2 = {"a", "b", "c","d"};
String[] authors3 = {"a", "b", "c","d","e","f"};
System.out.println(displayAuthors(authors0));
System.out.println(displayAuthors(authors1));
System.out.println(displayAuthors(authors2));
System.out.println(displayAuthors(authors3));
}
public static String displayAuthors(String[] authors) {
StringBuilder stringBuilder = new StringBuilder();
String stringAuthors="";
String prefixComma = ", ";
String prefixAnd = " and ";
if ((authors != null) && (authors.length > 0)) {
for (int i = 0; i < authors.length; i++) {
if (i < authors.length - 2) {
stringBuilder.append(authors[i]).append(prefixComma);
} else {
stringBuilder.append(authors[i]).append(prefixAnd);
}
}
// Java Remove extra Characters("and ") from String
stringAuthors = stringBuilder.substring(0, stringBuilder.length() - 4);
}
return stringAuthors;
}
}
答案1
得分: 2
import java.util.Arrays;
import java.util.List;
class SeprateAuthors {
public static void main(String[] args) {
String[] authors0 = {"a", "b"};
String[] authors1 = {"a", "b", "c"};
String[] authors2 = {"a", "b", "c", "d"};
String[] authors3 = {"a", "b", "c", "d", "e", "f"};
System.out.println(displayAuthors(authors0));
System.out.println(displayAuthors(authors1));
System.out.println(displayAuthors(authors2));
System.out.println(displayAuthors(authors3));
}
public static String displayAuthors(String[] authorsArray) {
List<String> authors = Arrays.asList(authorsArray);
if (authors.size() == 1) {
return authors.get(0);
} else {
String lastAuthor = authors.get(authors.size() - 1);
List<String> firstAuthors = authors.subList(0, authors.size() - 1);
String firstAuthorsString = String.join(", ", firstAuthors);
return firstAuthorsString + " and " + lastAuthor;
}
}
}
输出:
a and b
a, b and c
a, b, c and d
a, b, c, d, e and f
英文:
import java.util.Arrays;
import java.util.List;
class SeprateAuthors {
public static void main(String[] args) {
String[] authors0 = {"a", "b"};
String[] authors1 = {"a", "b", "c"};
String[] authors2 = {"a", "b", "c", "d"};
String[] authors3 = {"a", "b", "c", "d", "e", "f"};
System.out.println(displayAuthors(authors0));
System.out.println(displayAuthors(authors1));
System.out.println(displayAuthors(authors2));
System.out.println(displayAuthors(authors3));
}
public static String displayAuthors(String[] authorsArray) {
List<String> authors = Arrays.asList(authorsArray);
if (authors.size() == 1) {
return authors.get(0);
} else {
String lastAuthor = authors.get(authors.size() - 1);
List<String> firstAuthors = authors.subList(0, authors.size() - 1);
String firstAuthorsString = String.join(", ", firstAuthors);
return firstAuthorsString + " and " + lastAuthor;
}
}
}
This should work for you.
Output:
a and b
a, b and c
a, b, c and d
a, b, c, d, e and f
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论