trim string at the end of the string

前端 未结 6 831
时光说笑
时光说笑 2021-01-16 07:01

Hello I want to remove the last word from my sentence in C#. Here is my query:

\"SELECT * FROM People WHERE City = @City AND County = @County AND\"
         


        
6条回答
  •  一个人的身影
    2021-01-16 07:22

    string myString = "SELECT * FROM People WHERE City = @City AND County = @County AND";
    Console.WriteLine(myString.Substring(0, myString.LastIndexOf(' ')));
    

    But you may also consider building your AND-clause without the last AND from the beginning.

    List andConditions = new List();
    andConditions.Add("City = @City");
    andConditions.Add("County = @County");
    string myString = "SELECT * FROM People WHERE " + string.Join(" AND ", andConditions);
    

提交回复
热议问题