如何从特定单词中删除部分字符串?

Lea*_*sed 2 c# sql sql-server

下面是我的SQL查询:

select * from Table where col1 = @param1 and col2 = @param2
Run Code Online (Sandbox Code Playgroud)

现在我想删除where子句中的所有内容,因此预期输出如下所示:

select * from Table;
Run Code Online (Sandbox Code Playgroud)

码:

string str = "select * from Table where col1 = @param1 and col2 = @param2";

var str = sqlQuery.Replace("where", ""); // select * from Table col1 = @param1 and col2 = @param2
Run Code Online (Sandbox Code Playgroud)

Mig*_*oom 7

一种方法是使用IndexOfSubstring.

var sql = "select* from Table where col1 = @param1 and col2 = @param2";
var index = sql.IndexOf("where", StringComparison.OrdinalIgnoreCase);
var newSql = sql.Substring(0, index);
Run Code Online (Sandbox Code Playgroud)

您也可以使用最后Trim删除空白区域.这看起来像.

var newSql = sql.Substring(0, index).Trim();
Run Code Online (Sandbox Code Playgroud)