[英]How to use string.Substring(char, char) instead string.Substring(int, int)?
I made the extension method that returns a string between leftChar
and rightChar
: 我做了一个在
leftChar
和rightChar
之间返回一个字符串的扩展方法:
public static string Substring(this string This, char leftChar, char rightChar)
{
int i_left = This.IndexOf(leftChar);
if (i_left >= 0)
{
int i_right = This.IndexOf(rightChar, i_left);
if (i_right >= i_left)
{
return This.Substring(i_left + 1, i_right - i_left - 1);
}
}
return null;
}
When I invoke it up like this: 当我这样调用它时:
string str = "M(234:342);".Substring('(', ')');
I get the exception: 我得到了例外:
System.ArgumentOutOfRangeException: startIndex не может быть больше, чем длина строки.
System.ArgumentOutOfRangeException:startIndexнеможетбытьбольше,чемдлинастроки。 Имя параметра: startIndex в System.String.Substring(Int32 startIndex, Int32 length)
Имяпараметра:startIndexвSystem.String.Substring(Int32 startIndex,Int32 length)
The namespace of the extension method and the invoking code is correct. 扩展方法的命名空间和调用代码是正确的。 IntelliSense suggest me 3 extension methods (two basic and one mine).
IntelliSense建议我使用3种扩展方法(两种基本方法和一种方法)。
(extension) string string.Substring(char leftChar, char rightChar)
(扩展名)string string.Substring(char leftChar,char rightChar)
It is clear that string.Substring(int startIndex, int Length)
overlaps with my extension method because the compiler casts char to int automaticaly. 很明显
string.Substring(int startIndex, int Length)
与我的扩展方法重叠,因为编译器会自动将char转换为int。
Is there a way to force the compiler not to do the cast char
to int
? 有没有办法强制编译器不要将cast
char
为int
?
Substring2
) all works. Substring2
)都可以。 chars
to params char[]
, it does not work, if I invoke it like params, but of course works like array. chars
更改为params char[]
,它就不起作用,如果我像params一样调用它,但当然就像数组一样。 "M(234:342);".Substring('(', ')')
does not throw any compilation error, but still throw the exception. "M(234:342);".Substring('(', ')')
不会抛出任何编译错误,但仍然抛出异常。 chars
to strings
in the extension method it also works. chars
更改为strings
,它也可以。 This is my current favorite solution for this issue - to combine my Substring(char, char)
with Substring(string, string)
with checking lengths of input strings. Substring(char, char)
和Substring(string, string)
与检查输入字符串的长度相结合。 Is there a way to force the interpreter not to do the cast char to int?
有没有办法强制解释器不要将转换char转换为int?
No. When it encounters a method invocation executed against a target expression, the compiler first checks whether any instance methods on the target's declared type are compatible with the arguments. 当遇到针对目标表达式执行的方法调用时,编译器首先检查目标声明类型上的任何实例方法是否与参数兼容。 It only looks for extension methods if there are no compatible methods.
如果没有兼容的方法,它只查找扩展方法。
In your case, the regular string.Substring(int, int)
method is compatible with arguments (char, char)
because of the implicit conversion from char
to int
. 在您的情况下,常规
string.Substring(int, int)
方法与参数(char, char)
兼容,因为从char
到int
的隐式转换。
The simplest solution is to rename your extension method - something like SubstringBetween
perhaps. 最简单的解决方案是重命名扩展方法 - 也许类似于
SubstringBetween
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.