Question

I have

string query="Date >= '" + FromDate + "' AND Date <= '" + ToDate + "' OR ";

I want to remove OR from this string coming at last. How I Can remove this from string.

No correct solution

OTHER TIPS

query = query.Substring(0, query.LastIndexOf("OR"));

Try this:

query = query.Substring(0, query.LastIndexOf("OR"));

I suggest you:

  1. Use string formatting instead of string concatenations
  2. Not to add string which you need to remove

I.e.

var query = String.Format("Date >= '{0}' AND Date <= '{1}'", FromDate, ToDate);

BTW If you are building SQL query this way, then consider to use query parameters instead:

var query = "Date >= @fromDate AND Date <= @toDate";

try replace last occurence:

query = query.Substring(0, query.LastIndexOf("OR"));

or remove:

query= query.Remove(str.LastIndexOf("OR"), 2);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top