简体   繁体   中英

How to remove LAST INSTANCE of a character from a string

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')

您可以使用“删除”方法删除某个位置的特定字符:

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

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.

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.

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

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

                  } 

Replace

            Q.Append(","); 

inside the for-loop with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM