gpt4 book ai didi

java - 将连接的字符串按每组 160 个字符进行分组,并对剩余的字符串执行相同的操作

转载 作者:行者123 更新时间:2023-12-02 04:26:24 24 4
gpt4 key购买 nike

我有一个表,我想要检索数据。我设法检索这些数据。问题是我想连接这些数据(字符串)以创建一个包含 160 个字符的字符串。我设法通过让 Stackoverflow 中的一个人回答此代码来做到这一点。

代码是这样的:

List<Pending> pending = db.getAllPending();
String a = "";
for (Pending pn : pending) {
if (a.length() + pn.getPm_str().length() <= 160) {
a += pn.getPm_str();
}
else
break;
}

The code concatenates the data until it has formed a 160 length string. It will disregard the other data which happened to exceed with the limit.

我的问题是:我该如何对列表中的剩余数据(不包含在第一批 160 长度字符串中)执行相同的操作?我还希望将这些数据连接起来以创建相同的数据。

该程序的概念与短信发送有关。数据被连接并分组为一条短信发送。

需要帮助。

最佳答案

您需要创建一个结果字符串列表,并在连接字符串达到限制时将其添加到其中:

List<Pending> pending = db.getAllPending();
List<String> resultingStrings = new ArrayList<String>();
String a = "";
for (Pending pn : pending) {
if (a.length() + pn.getPm_str().length() <= 160) {
a += pn.getPm_str();
}
else {
resultingStrings.add(a);
a = pn.getPm_str();
}
}
resultingStrings.add(a);

关于java - 将连接的字符串按每组 160 个字符进行分组,并对剩余的字符串执行相同的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32094740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com