我有点疑惑为什么Substring()函数无法正常工作。它只是不会删除字符串末尾的逗号。
请参阅下面的示例代码......
public static string OrderByClauseBuilder(string parmSortByColumn)
{
if (parmSortByColumn.LastIndexOf(",") > -1) {
parmSortByColumn.Substring(0, parmSortByColumn.LastIndexOf(","));
}
return parmSortByColumn;
}
protected void Page_Load(object sender, EventArgs e)
{
string sqlAsk = "";
string value = "stocknumber asc,";
sqlAsk = OrderByClauseBuilder(value);
}
答案 0 :(得分:7)
字符串在.NET中是不可变的,并且每个字符串方法都返回一个新的,经过修改的方法。因此,您必须做一些调整:
public static string OrderByClauseBuilder(string parmSortByColumn)
{
if (parmSortByColumn.LastIndexOf(",") > -1) {
parmSortByColumn = parmSortByColumn.Substring(0, parmSortByColumn.LastIndexOf(","));
}
return parmSortByColumn;
}