连接数组并从Firestore中获取字符串

huangapple go评论79阅读模式
英文:

concatenate array to string from firestore

问题

我正在Firestore中存储一些数据,看起来如下所示:

连接数组并从Firestore中获取字符串

我现在想要获取这些数据并创建一个单一的字符串,如下所示:

David John Josh

用空格分隔单词。

我通过以下方式获取这些数据:

queryNames.get().addOnCompleteListener(task -> {
    if (task.isSuccessful()) {
        QuerySnapshot snapshot = task.getResult();
        for (int i = 0; i < snapshot.size(); i++){
            String Names = snapshot.getDocuments().get(i).get("Names");
        }
        if (snapshot.isEmpty()){

        }
    }
});

如何将这些单词连接成一个单一的字符串?

谢谢

英文:

I am storing some data in Firestore which looks as follows:

连接数组并从Firestore中获取字符串

I had like now to get this data and create a single string as:

David John Josh

Separated with space in between the words.

I get this data by using:

queryNames.get().addOnCompleteListener( task -&gt; {
    if (task.isSuccessful()) {
        QuerySnapshot snapshot = task.getResult();
        for (int i = 0; i &lt; snapshot.size(); i++){
            String Names = snapshot.getDocuments().get( i ).get( &quot;Names&quot; ) );
        }
        if (snapshot.isEmpty()){

        }

    }
} );

How can I concatenate this words for a single string?

Thank you

答案1

得分: 0

可以使用 StringBuilder 来将所有的名称拼接在一起。

StringBuilder sb = new StringBuilder();
for (int i = 0; i < snapshot.size(); i++){
    if(i != 0) sb.append(' ');
    String name = snapshot.getDocuments().get(i).get("Names");
    sb.append(name);
}
String result = sb.toString();
英文:

You can use a StringBuilder to concatenate all the names together.

StringBuilder sb = new StringBuilder();
for (int i = 0; i &lt; snapshot.size(); i++){
    if(i != 0) sb.append(&#39; &#39;);
    String name= snapshot.getDocuments().get( i ).get( &quot;Names&quot; ) ;
    sb.append(name);
}
String result = sb.toString();

答案2

得分: 0

我不太了解Firestore的工作原理但如果你需要连接字符串你可以这样做
queryNames.get().addOnCompleteListener(task -&gt; {
    if (task.isSuccessful()) {
        QuerySnapshot snapshot = task.getResult();
        String result = "";
        for (int i = 0; i < snapshot.size(); i++){
            result += snapshot.getDocuments().get(i).get("Names") + " ";
        }
        if (snapshot.isEmpty()){

        }
    }
});
或者你可以
 - 使用StringBuilder而不是Java的字符串连接
 - 将数据存储在`List<String>`并使用`list.stream.collect(Collectors.joining(" "))`。
英文:

Well, I don't know how works Firestore, but if you need to concatenate, then you can do something like that:

queryNames.get().addOnCompleteListener( task -&gt; {
    if (task.isSuccessful()) {
        QuerySnapshot snapshot = task.getResult();
        String result = &quot;&quot;;
        for (int i = 0; i &lt; snapshot.size(); i++){
            result += snapshot.getDocuments().get( i ).get( &quot;Names&quot; ) ) + &quot; &quot;;
        }
        if (snapshot.isEmpty()){

        }
    }
} );

Alternatively, you could:

  • Use a StringBuilder instead of java concatenation.
  • Store the data in a List&lt;String&gt; and use list.stream.collect(Collectors.joining(&quot; &quot;))

答案3

得分: 0

当您从文档中获取Names字段时,您会得到一个数组或List

因此,您可以遍历该数组,并使用以下方法连接各个字符串/名称:

queryNames.get().addOnCompleteListener(task -> {
    if (task.isSuccessful()) {
        for (QueryDocumentSnapshot document : task.getResult()) {
            // 从文档中获取名称列表
            List<String> namesList = (List<String>) document.get("Names");

            // 将所有名称连接成一个字符串
            String names = String.join(", ", namesList);
        }
    }
});
英文:

When you get the Names field from the document, you get back an array or List.

So you can iterate over that and concatenate the individual strings/names with:

queryNames.get().addOnCompleteListener( task -&gt; {
    if (task.isSuccessful()) {
        for (QueryDocumentSnapshot document : task.getResult()) {
            // Get the list of names from the document
            List&lt;String&gt; namesList = (List&lt;String&gt;) document.get(&quot;Names&quot;);

            // Concatenate all the names into a single string
            String names = String.join(&quot;, &quot;, namesList)
        }
    }
});

huangapple
  • 本文由 发表于 2020年7月25日 21:59:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63089259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定