[英]Convert function PHP to C#
我陷入了PHP函数到C#的转换中!
有人可以告诉我我做的不好吗?
原始代码(PHP):
function delete_all_between($beginning, $end, $string)
{
$beginningPos = strpos($string, $beginning);
$endPos = strpos($string, $end);
if ($beginningPos === false || $endPos === false) {
return $string;
}
$textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos);
return str_replace($textToDelete, '', $string);
}
我的代码C#:
string delete_all_between(string beginning, string end, string html)
{
int beginningPos = html.IndexOf(beginning);
int endPos = html.IndexOf(end);
if (beginningPos == -1 || endPos == -1)
{
return html;
}
}
希望有人能帮助我,我真的很困!
使用“ Substring
和“ Replace
掩盖并替换不需要的字符串。 另外,我将添加检查以确保endPos > beginningPos
:
string delete_all_between(string beginningstring, string endstring, string html)
{
int beginningPos = html.IndexOf(beginningstring);
int endPos = html.IndexOf(endstring);
if (beginningPos != -1 && endPos != -1 && endPos > beginningPos)
{
string textToDelete = html.Substring(beginningPos, (endPos - beginningPos) + endstring.Length); //mask out
string newHtml = html.Replace(textToDelete, ""); //replace mask with empty string
return newHtml; //return result
}
else return html;
}
输入测试
delete_all_between("hello", "bye", "Some text hello remove this byethat I wrote")
产生结果:
我写的一些文字
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.