prasanna
prasanna

Reputation: 51

Converting string[] into comma separated string.

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"

Upvotes: 1

Views: 1923

Answers (4)

user268396
user268396

Reputation: 12006

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();
}

Upvotes: 1

Marcelo
Marcelo

Reputation: 2302

// 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.

Upvotes: 0

Thilo
Thilo

Reputation: 262842

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.

Upvotes: 0

Vinay
Vinay

Reputation: 61

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"?

Upvotes: 0

Related Questions