将 string[] 转换为逗号分隔的字符串。
我有一个字符串数组,我需要将其解析为长变量。
字符串数组包含 ["9", "8"] 等值。 我想将其转换为“(9,8)”(我在 SQL 语句的“IN”子句中使用它)。
实现这一目标的好方法是什么?
这是我的代码:
String[] statusIdArray;
long statsId = Long.parseLong("(");
statsId = Long.parseLong(statusIdArray[0]);
for(int i=1; i < len; i++) {
statsId = Long.parseLong(statsId + ",") statsId=Long.parseLong(statsId + statusIdArray[i]);
statsId = Long.parseLong(statsId + ")");
}
但是我在输入字符串“(9”处得到了 NumberFormatException
I have a string array which i need to parse it to long variable.
The string array contains values as ["9", "8"] etc.
I want to convert this to "(9,8)" (I am using it in the "IN" clause of an SQL statement).
What is a good way to accomplish this?
Here is my code:
String[] statusIdArray;
long statsId = Long.parseLong("(");
statsId = Long.parseLong(statusIdArray[0]);
for(int i=1; i < len; i++) {
statsId = Long.parseLong(statsId + ",") statsId=Long.parseLong(statsId + statusIdArray[i]);
statsId = Long.parseLong(statsId + ")");
}
But I get a NumberFormatException at input string "(9"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设问题是:我有一个字符串数组,我想将其转换为 SQL 表示法,因此我可以在 SQL 查询中使用它们:
Assuming that the question is: I have an array of Strings which I want to convert to SQL notation so I may use these in a SQL query:
你们通过“将此字符串数组解析为 (9,8) 长”来做什么?
你有字符串数组吗?或者只是一根字符串?您是否希望将每个数字(以逗号分隔)输入到 Long 数组中?或者你希望 (9,8) 变成长“98”?
What do you men by "parse this string array to long as (9,8)?"
Do you have an array of Strings? Or just one String? Do you want each digit (separated by a comma) to be entered into a Long array? Or would you want (9,8) to become a Long "98"?
问题有点不清楚,但也许
在这种情况下使用 Jakarta Commons StringUtils (或其他带有 join 方法的东西)
我通常也完全反对将变量直接插值到 SQL 中(你应该使用绑定变量),但这是一个可变长度 IN 列表有点棘手。只要确保您不会在该字符串数组中获得奇怪的数据即可。
Question is a bit unclear but maybe
In this case use Jakarta Commons StringUtils (or something else with a join method)
I am usually also totally against direct interpolation of variables into SQL (you should use bind variables), but that is a bit tricky with a variable-length IN list. Just make sure you don't get weird data in that String array.
我想我现在就明白了。
您必须小心数组的内容,您可能会成为 SQL 注入攻击的受害者。
I think I got it right now.
You have to be carefull with the contents of the array, you could be a victim of an SQL Injection attack.