7

I am building a string with StringBuilder.

StringBuilder Q = new StringBuilder();
Q.Append("INSERT INTO ");
Q.Append(_lstview_item);
Q.Append(" VALUES");
Q.Append("("); 
for (i = 0; i < col_no; i++)
{
    Q.Append("'");
    Q.Append(col_value[i]);
    Q.Append("'");
    Q.Append(",");
} 
Q.Append(")");
string query = Q.ToString();

However, I am getting a "," at the end of my string. I tried using

string query = ext.Substring(0, ext.LastIndexOf(",") + 1);

to remove the surplus ",", but this also removes the ")".

How can I remove the last comma only?

actual outcome : INSERT INTO .... VALUES('1','2','3',)

desired outcome : INSERT INTO .... VALUES('1','2','3')

8
  • 5
    It's not answering your question, but I really encourage you to use parameterized queries rather than generating them by concatenating strings. There are a tonne of posts on SO about how to do this. Aug 20, 2013 at 7:52
  • Why add you Q.Append(")"); at all if you want to remove the comma first? Aug 20, 2013 at 7:52
  • 2
    The solution isn't to remove the comma, but to do this differently. Aug 20, 2013 at 7:52
  • @TimSchmelter With such reasoning one might ask why it isn't only conditionally added within the loop. Aug 20, 2013 at 7:53
  • 3
    You are always a bobby tables away from ruin!
    – xanatos
    Aug 20, 2013 at 7:55

6 Answers 6

18

You can use the "Remove" method to remove a specific character at a position:

query = query.Remove(query.LastIndexOf(","), 1);
2

This:

Q.Append(")");

replace with

if (col_no > 0)
{
    Q.Length--;
}

Q.Append(")");

The check if (col_no > 0) is a little overboard, because if there is no column, the query will still fail for other reasons, but if we consider this a template on how to combine strings in a StringBuilder, then the check is the right thing to do.

Ah... Building a query in that way is the wrong thing to do.

0
1

I would suggest to remove the comma first before you add the last ), so:

for (i = 0; i < col_no; i++)
{
    Q.Append("'");
    Q.Append(col_value[i]);
    Q.Append("'");
    Q.Append(",");

} 
if(col_no > 0) Q.Length --; // <-- this removes the last character
Q.Append(")");
string query = Q.ToString();

However, if you really want to create a sql-query i would strongly suggest to use sql-parameters to prevent sql-injection. So don't include the values in your sql-string.

0
  1. the correct way to achieve your goal is to use a parameterized query.
  2. there is the possibility to delete the last coma before putting the bracket
  3. the pure answer to your question can be this:

.

string query = ext.Substring(0, ext.LastIndexOf(",")) + ext.Substring(ext.LastIndexOf(",") + 1);

or this:

string query = ext.Remove(ext.LastIndexOf(","), 1);
0

Try this. simple logic

Check the below if condition in befor append comma line

                  **if(i!=(col_no-1))
                    {
                    Q.Append(",");
                    }*

With your code .

                 for (i = 0; i < col_no; i++)
                  {
                    Q.Append("'");
                    Q.Append(col_value[i]);
                    Q.Append("'");
                    **if(i!=(col_no-1))
                    {
                    Q.Append(",");
                    }**

                  } 
0

Replace

            Q.Append(","); 

inside the for-loop with

            if (i != col_no - 1)
            {
                Q.Append(",");
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.