我正在编写文件。我在循环中有一个字符串数组 我需要为在文件上写入的所有值附加双引号。任何人都可以帮我如何为String Array中的所有值附加双引号。提前谢谢。
答案 0 :(得分:1)
我想您可能正在寻找的内容如下:
region.append("\"").append(a[z]).append("\"").append(',');
答案 1 :(得分:0)
如果您正在使用Java 8,那么您可能想要做的是
String region = Arrays.stream(a)
.map(s -> String.format("\"%s\"", s)) // add double quotes around each string
.collect(Collectors.joining(","); // comma-separate values
答案 2 :(得分:0)
例如:
int l = myStrings.length;
for(int i = 0; i < l; i++){
// Adds " to the begning of the string and ", to the end of the string.
myStrings[i] = "\"" + mystrings[i] + "\",";
// if you want to use String.format:
//myStrings[i] = String.format("\"%s\",", myStrings[i]);
// if it is the last string, remove the unwanted comma
if(i == l-1){
// gets the substring of the last string in the array, excluding only
// the last character, because it is an unwanted comma
myStrings[i] = myStrings[i].substring(0,l);
}
// Write in your file here
// region.append(myStrings[i]);
}