简体   繁体   English

将循环结果另存为字符串

[英]Save for loop result as string

    String input[] = request.getParameterValues("checkbox");

        for(int i=0;i<input.length;i++) {
            if (i==input.length-1) {
                System.out.print(input[i]+" ");
            } else {
                    System.out.print(input[i]+", ");
            }
        }

The result is that i print to console something like "CustomerId, FirstName, LastName, Phone". 结果是我打印到控制台,例如“ CustomerId,FirstName,LastName,Phone”。 I want to save the result of the for loop as a string variable instead, so then i can do 我想将for循环的结果另存为字符串变量,所以我可以

     String query = "select "+result above+" from table";

How to do it? 怎么做?

You keep appending into a StringBuilder and then convert it to String 您继续追加到StringBuilder ,然后将其转换为String

StringBuilder sb = new StringBuilder();
for(int i=0;i<input.length;i++) {
  if (i==input.length-1) {
     System.out.print(input[i]+" ");
     sb = sb.concat(input[i] + ",");
  } else {
     System.out.print(input[i]+", ");
     sb = sb.concat(input[i] + ",");
  }
}

Then, 然后,

String query = "select "+sb.toString()+" from table";

Use a StringBuilder to concatenate strings together. 使用StringBuilder将字符串连接在一起。 (it is technically possible to use String objects, and concatenate them together using the + operator, but that has a lot of downsides...) (从技术上讲,可以使用String对象,并使用+运算符将它们连接在一起,但这有很多缺点...)

    String input[] = request.getParameterValues("checkbox");

    StringBuilder sb = new StringBuilder(); // create empty StringBuilder instance
    for(int i=0;i<input.length;i++) {
        sb.append(input[i]);     //append element
        if (i==input.length-1) {
            sb.append(" ");      //append space
        } else {
            sb.append(", ");     //append comma
        }
    }

    String result = sb.toString();
    Systemout.println(result);

Or you could build ( see Builder pattern ) the whole query using a StringBuilder object, with methods for each part ( addFields(StringBuilder sb) , addFromPart(StringBuilder sb) , addWhereClause(StringBuilder sb) ), and voila, you have the insides of a small data access framework... 或者,您可以使用StringBuilder对象构建( 请参阅Builder模式 )整个查询,并使用每个部分的方法( addFields(StringBuilder sb)addFromPart(StringBuilder sb)addWhereClause(StringBuilder sb) )和瞧,您可以了解一个小的数据访问框架

public abstract class MyQueryBuilder {

    protected abstract void addFields(StringBuilder sb);
    protected abstract void addFromPart(StringBuilder sb);
    protected abstract void addWhereClause(StringBuilder sb);

    public final String getQuery() {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT ");

        addFields(sb); //this adds the fields to be selected
        sb.append(" FROM ");

        addFromPart(sb); //this adds the tables in the FROM clause

        addWhereClause(sb); //this adds the where clause
        //...etc
        return sb.toString();
    }
}

You can do this using a StringBuilder instead of printing the values out. 您可以使用StringBuilder来执行此操作,而不是打印出值。

String input[] = request.getParameterValues("checkbox");

    StringBuilder builder = new StringBuilder();

    for(int i=0;i<input.length;i++) {
        if (i==input.length-1) {
            builder.append(input[i]+" ");
        } else {
                builder.append(input[i]+", ");
        }
    }

Regards, kayz 问候,凯兹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 for 循环中的数组结果保存到变量 - Save array result from a for loop to a variable 将循环结果放入Java中的字符串中 - Put loop result in a string in java 如何将循环中的结果转换为带索引的字符串? - how to take the result from the loop into string with index? 在 ArrayList 中搜索单词<String>并将结果保存到 Map<String, Integer> - Search for words in a ArrayList<String> and save result to Map<String, Integer> 如何循环结果集并将其保存在地图列表中 - How to loop a result-set and Save it in list of maps TreeMap的for-each循环中的getValue结果 <String, Set<String> &gt; - Result of getValue in for-each loop for TreeMap<String, Set<String>> 如何将Float结果转换为String并将其保存到Firebase数据库 - How to convert Float Result into String and save it into Firebase database 如何在Java中比较字符串并将结果保存在字符串数组中? - how to compare strings in java and save the result in string array? 为什么我得到这个结果? 简单循环以反转字符串 - Why am I getting this result? Simple loop to reverse a string 需要帮助:我的循环中某些功能无法正常运行。 我相信结果+结果字符串会引起问题吗? - Need help: Something is not functioning right in my loop. I believe the result + result string is causing issues?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM