Java 如何使用字符串生成器在元素之间附加双引号和“或”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22682016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:15:51  来源:igfitidea点击:

how to use string builder to append double quotes and 'OR' between elements

javaarrays

提问by user3467677

This is my code where I will get set of values in a list array.

这是我的代码,我将在其中获取列表数组中的一组值。

how to use string builder to append double quotes and OR between all those elements and save to a string?

如何使用字符串生成器在所有这些元素之间附加双引号和 OR 并保存到字符串?

Also is it possible to get the elements in a split? For example I will get:

也有可能在一个分裂中获得元素吗?例如我会得到:

a,b,c,d,e,f,g,h,i,j 

in an array I want the output as

在一个数组中,我希望输出为

"a" OR "b" OR "c" OR "d" 

and remaining elements in another iteration. As there are 10 elements I want 5 elements at at time in above format. Is it possible in string builder. Here in my example I have mentioned total as 10 elements but I actually get more than 1000 elements. Here is my code.

和另一个迭代中的剩余元素。由于有 10 个元素,我一次需要 5 个元素以上述格式。是否可以在字符串生成器中使用。在我的示例中,我提到 total 为 10 个元素,但实际上我得到了 1000 多个元素。这是我的代码。

List<String> test = new ArrayList<String>();
while (rs.next()) {
    String url = rs.getString("rssUrl");
    test.add(url);
} 

采纳答案by Florent Bayle

Using StringBuilderis a good idea when concatenating in a loop, as it will have better performances than the simple Stringconcatenation. Here is a sample code as how to do it, using StringBuilderand modulo operation to check how many elements you want in the result:

StringBuilder在循环中连接时使用是一个好主意,因为它比简单的String连接具有更好的性能。这是一个示例代码,说明如何执行此StringBuilder操作,使用模运算来检查结果中需要多少个元素:

final int nbElems = 5;
List<String> test = new ArrayList<String>(Arrays.asList("a,b,c,d,e,f,g,h,i,j".split(",")));

StringBuilder sb = null;
for (int i=0; i<test.size(); i++) {
    if (i%nbElems==0)
        sb = new StringBuilder("\"");
    else
        sb.append("\" OR \"");
    sb.append(test.get(i));
    if (i%nbElems==(nbElems-1) || i==(test.size()-1)) {
        sb.append("\"");
        final String result = sb.toString();
        System.out.println(result); // TODO: do something
    }
}

Output:

输出:

"a" OR "b" OR "c" OR "d" OR "e"
"f" OR "g" OR "h" OR "i" OR "j"

回答by Roberto

This is a way to get it, there are some others:

这是一种获取方式,还有一些其他方式:

public static void main(String[] args) {
    List<String> list = Arrays.asList("a","b","c","d","e","f","g","h","i","j");
    StringBuilder sb = new StringBuilder(String.format("\"%s\"", list.get(0)));
    for (int i = 1; i < list.size(); i++) {
         sb.append(" OR ").append(String.format("\"%s\"", list.get(i)));
    }

    System.out.println(sb.toString());
}

The output should be:

输出应该是:

"a" OR "b" OR "c" OR "d" OR "e" OR "f" OR "g" OR "h" OR "i" OR "j"

回答by cheseaux

Here is my contribution using split and String.

这是我使用 split 和String.

String[] letters = "a,b,c,d,e,f,g,h,i,j".split(",");
String concat = "";
int index = 0;
int itemPerLine = 5;

while(index < letters.length) {
    concat += "\"" + letters[index] + "\"";
    index++;
    if (index%itemPerLine == 0 || index==letters.length){
        System.out.println(concat);
        concat = "";
    } else {
        concat += " OR ";
    }
}

As requested, it outputs :

根据要求,它输出:

"a" OR "b" OR "c" OR "d" OR "e"
"f" OR "g" OR "h" OR "i" OR "j"

回答by Autocrab

String base = "a,b,c,d,e,f,g,h,i,j";
String[] letters = base.split(",");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < letters.length; i++) {
    if (i > 0)
       builder.append(" OR ");
    builder.append("\"").append(letters[i]).append("\"");
}