将 string[] 转换为逗号分隔的字符串。

发布于 2024-10-11 21:33:11 字数 508 浏览 5 评论 0原文

我有一个字符串数组,我需要将其解析为长变量。

字符串数组包含 ["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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吃素的狼 2024-10-18 21:33:11

假设问题是:我有一个字符串数组,我想将其转换为 SQL 表示法,因此我可以在 SQL 查询中使用它们:

import java.util.Arrays; // may need to add this import somewhere among your import statements

/**
 * Convert Java String[] to SQL array with varargs syntax.
 * Example method invocations:<blockquote><pre>
 * String sql1 = toSQLArray("1","2","3"); // should return "(1,2,3)"
 * String sql2 = toSQLArray(new String[] { "0", "5" }); // should return "(0,5)"
 * </pre></blockquote>
 * @param array string array.
 * @return String created by concatenating each array element using "," as separator and 
 * adding "(" and ")" as delimiters for the result. 
 */
public String toSQLArray(String... array) {
  StringBuilder sb= new StringBuilder(Arrays.toString(array));
  sb.replace(0,1, "(");
  int l = sb.length();
  sb.replace(l-1, l, ")");
  return sb.toString();
}

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:

import java.util.Arrays; // may need to add this import somewhere among your import statements

/**
 * Convert Java String[] to SQL array with varargs syntax.
 * Example method invocations:<blockquote><pre>
 * String sql1 = toSQLArray("1","2","3"); // should return "(1,2,3)"
 * String sql2 = toSQLArray(new String[] { "0", "5" }); // should return "(0,5)"
 * </pre></blockquote>
 * @param array string array.
 * @return String created by concatenating each array element using "," as separator and 
 * adding "(" and ")" as delimiters for the result. 
 */
public String toSQLArray(String... array) {
  StringBuilder sb= new StringBuilder(Arrays.toString(array));
  sb.replace(0,1, "(");
  int l = sb.length();
  sb.replace(l-1, l, ")");
  return sb.toString();
}
岁吢 2024-10-18 21:33:11

你们通过“将此字符串数组解析为 (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"?

病女 2024-10-18 21:33:11

问题有点不清楚,但也许

long[] result = new long[ strings.length ];
for (int i=0; i<result.length; i++){
    result[i] = Long.parseLong( strings[i] );
}

我有一个由数字组成的字符串数组。例如 (9 8) 我必须将此字符串数组作为长变量传递给 sql 的“IN”子句。当很长一段时间过去了

在这种情况下使用 Jakarta Commons StringUtils (或其他带有 join 方法的东西)

sql.append("IN (");
sql.append(StringUtils.join(strings, ','));
sql.append(")");

我通常也完全反对将变量直接插值到 SQL 中(你应该使用绑定变量),但这是一个可变长度 IN 列表有点棘手。只要确保您不会在该字符串数组中获得奇怪的数据即可。

Question is a bit unclear but maybe

long[] result = new long[ strings.length ];
for (int i=0; i<result.length; i++){
    result[i] = Long.parseLong( strings[i] );
}

i have a string array which consists of digits.say example (9 8) i have to pass this string array as a long variable to the "IN" clause of sql.it should be like (9,8) when the long is passed

In this case use Jakarta Commons StringUtils (or something else with a join method)

sql.append("IN (");
sql.append(StringUtils.join(strings, ','));
sql.append(")");

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.

别闹i 2024-10-18 21:33:11
// digits is your array
String result = "IN (";
for (int i = 0; i < digits.length; i++) {
    result += digits[i] + (i != digits.lenght - 1) ? ", " : "";
}

result += ")";

我想我现在就明白了。

您必须小心数组的内容,您可能会成为 SQL 注入攻击的受害者。

// digits is your array
String result = "IN (";
for (int i = 0; i < digits.length; i++) {
    result += digits[i] + (i != digits.lenght - 1) ? ", " : "";
}

result += ")";

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文