[英]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.