[英]Convert function PHP to C#
I am stuck in a convertion of PHP function to C#! 我陷入了PHP函数到C#的转换中!
Someone can tell me what I am doing bad? 有人可以告诉我我做的不好吗?
Original code (PHP): 原始代码(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);
}
My code C#: 我的代码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;
}
}
I hope someone can help me, I am really stuck! 希望有人能帮助我,我真的很困!
Use Substring
and Replace
to mask out and replace the unwanted string. 使用“ Substring
和“ Replace
掩盖并替换不需要的字符串。 Also, I would add a check to make sure that endPos > beginningPos
: 另外,我将添加检查以确保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;
}
Test with input 输入测试
delete_all_between("hello", "bye", "Some text hello remove this byethat I wrote")
Yields the result: 产生结果:
Some text that I wrote 我写的一些文字
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.