由于字符串: /Projects/Multiply_Amada/MultiplyWeb/Shared/Home.aspx
我想之后的第三 /
,这样的结果是去除所有尾随字符: /项目/ Multiply_Amada /
I want to remove all trailing characters after the third /
, such that the result is: /Projects/Multiply_Amada/
我想做到这一点,而无需使用拆分或CHARINDEX。
I would like to do this without using Split or Charindex.
OK,你的要求是有点困难。那么,是什么样:
OK, your requirements are a bit tough. So, what about this:
string RemoveAfterThirdSlash(string str)
{
return str.Aggregate(
new {
sb = new StringBuilder(),
slashes = 0
}, (state, c) => new {
sb = state.slashes >= 3 ? state.sb : state.sb.Append(c),
slashes = state.slashes + (c == '/' ? 1 : 0)
}, state => state.sb.ToString()
);
}
Console.WriteLine(RemoveAfterThirdSlash("/Projects/Multiply_Amada/MultiplyWeb/Shared/Home.aspx"));