2017-09-28 105 views
0

我正在写文件。我有一个字符串数组作为循环中 我需要追加所有正在写它的值的双引号文件。任何一个可以帮助我如何在字符串Array.Thanks所有值双引号追加在advance`如何追加双引号(“”)对于在java中的字符串数组Variable

+0

你有没有尝试过任何事情有关系吗? –

+0

Yead,我试过我能够将双引号附加到我的数组值,我试过这个“\”“+ a [x] +”\“”。 感谢大家的投入。 – veda

回答

1

我猜你可能会寻找如下:

region.append("\"").append(a[z]).append("\"").append(','); 
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 
+0

这只有在他使用Java 8 – araknoid

+0

@araknoid时才有效,因为OP没有显示任何问题,所以当涉及到答案时,这实际上是免费的。 –

+0

@ M.Prokhorov你是对的,但由于java版本没有在问题中指定,它至少应该在答案中。 – araknoid

0
  1. 每串,围绕着它加上引号
  2. 每串,除了最后一个,添加逗号到结束
  3. 写入文件中的所有字符串。

例如:

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]); 
} 
相关问题