在Java中如何在字符串数组之间添加逗号和”and”?

huangapple go评论80阅读模式
英文:

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实体字符(如&quot;)转换为普通的双引号(")以获得更清晰的代码。

英文:

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 = {&quot;a&quot;, &quot;b&quot;};
String[] authors1 = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;};
String[] authors2 = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;,&quot;d&quot;};
String[] authors3 = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;,&quot;d&quot;,&quot;e&quot;,&quot;f&quot;};
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=&quot;&quot;;
String prefixComma = &quot;, &quot;;
String prefixAnd = &quot; and &quot;;
if ((authors != null) &amp;&amp; (authors.length &gt; 0)) {
for (int i = 0; i &lt; authors.length; i++) {
if (i &lt; authors.length - 2) {
stringBuilder.append(authors[i]).append(prefixComma);
} else {
stringBuilder.append(authors[i]).append(prefixAnd);
}
}
//  Java Remove extra Characters(&quot;and &quot;) 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 = {&quot;a&quot;, &quot;b&quot;};
String[] authors1 = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;};
String[] authors2 = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;};
String[] authors3 = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;, &quot;f&quot;};
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&lt;String&gt; authors = Arrays.asList(authorsArray);
if (authors.size() == 1) {
return authors.get(0);
} else {
String lastAuthor = authors.get(authors.size() - 1);
List&lt;String&gt; firstAuthors = authors.subList(0, authors.size() - 1);
String firstAuthorsString = String.join(&quot;, &quot;, firstAuthors);
return  firstAuthorsString + &quot; and &quot; + 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

huangapple
  • 本文由 发表于 2020年8月8日 00:07:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63305481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定