英文:
concatenate array to string from 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:
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 -> {
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()){
}
}
} );
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 < snapshot.size(); i++){
if(i != 0) sb.append(' ');
String name= snapshot.getDocuments().get( i ).get( "Names" ) ;
sb.append(name);
}
String result = sb.toString();
答案2
得分: 0
我不太了解Firestore的工作原理,但如果你需要连接字符串,你可以这样做:
queryNames.get().addOnCompleteListener(task -> {
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 -> {
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()){
}
}
} );
Alternatively, you could:
- Use a StringBuilder instead of java concatenation.
- Store the data in a
List<String>
and uselist.stream.collect(Collectors.joining(" "))
答案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 -> {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
// Get the list of names from the document
List<String> namesList = (List<String>) document.get("Names");
// Concatenate all the names into a single string
String names = String.join(", ", namesList)
}
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论