简体   繁体   English

修改string.Substring

[英]Modifying string.Substring

9 times out of 10, when I want to use the Substring() method on a string, it's because I want to shave some characters off the END of the string, not the start. 10次​​中有9次,当我想在Substring()上使用Substring()方法时,这是因为我想要从字符串的END中删除一些字符,而不是开始。 While the default Substring does have support for this, it does so by taking a second parameter which is the length of the desired string, rather than the endpoint. 虽然默认的Substring确实支持这一点,但它是通过获取第二个参数来实现的,该参数是所需字符串的长度,而不是端点。 This means if I consistently want to shave off N characters off of a series of strings of differing length, I have to do an extra step, which can result in a good deal more code. 这意味着如果我一直想要从一系列不同长度的字符串中删除N个字符,我必须做一个额外的步骤,这可以产生更多的代码。 Take for example: 举个例子:

//Shaving the first N chars
string trimmed = foo.bar.widget.GetType().Name.Substring(N);

vs.

//Shaving the last N chars
string trimmed = foo.bar.widget.GetType().Name.Substring(0, foo.bar.widget.GetType().Name.Length - N);

or maybe to save the extra function call, use a second line: 或者可能要保存额外的函数调用,请使用第二行:

string name = foo.bar.widget.GetType().Name;
string trimmed = name.Substring(0, name.Length - N);

Either way, you're basically doubling the amount of code necessary to shave characters off the end of the string rather than the beginning. 无论哪种方式,你基本上将字符串末尾的字符数量削减所需的代码量增加一倍,而不是开头。 My modification would be simple. 我的修改很简单。 If you pass a negative N (which would otherwise be meaningless), it would shave -N characters off the end of the string instead of the beginning. 如果你传递一个负N(否则会没有意义),它会在字符串末尾而不是开头处削减-N字符。 I can already code this up with an extension method: 我已经可以使用扩展方法对其进行编码:

public static string MySubstring(this string str, int val)
{
    return (val > 0) ? str.Substring(val) : str.Substring(0, str.Length + val);
}

And then when I want to shave off the final N chars, I just do: 然后,当我想要削减最后的N个字符时,我只是这样做:

string trimmed = foo.bar.widget.GetType().Name.MySubstring(-N);

Short and sweet, just like shaving off the beginning characters. 简短而甜蜜,就像剃掉了开头的人物一样。 My question is - would it be possible to override the behavior of the default Substring() function so that I can do this without having to use my own unique name for the function? 我的问题是 - 是否可以覆盖默认的Substring()函数的行为,这样我就可以这样做,而不必使用我自己的函数唯一名称? It's not like it would invalidate any existing code, because previously there was no reason to pass it a negative number, and doing so would simply throw an exception and crash. 它不会使任何现有代码无效,因为以前没有理由将它传递给负数,这样做只会引发异常并崩溃。 This is just one of those simple no-nonsense features that feels like it should've been part of the implementation to begin with. 这只是那些简单的严肃的功能之一,感觉它应该是开始实现的一部分。

According to C# documentation, you can use extension methods to extend a class or interface, but not to override them. 根据C#文档, 您可以使用扩展方法来扩展类或接口,但不能覆盖它们。 An extension method with the same name and signature as an interface or class method will never be called. 永远不会调用与接口或类方法具有相同名称和签名的扩展方法。 So the answer is "No". 所以答案是“不”。

Arguably, this is a good thing™, because otherwise your code would become a nightmare to read to someone not familiar with your extension. 可以说,这是一件好事,因为否则你的代码会变成一个噩梦,可以读给不熟悉你的扩展的人。

Note: str.Substring(0, str.Length + val); 注意: str.Substring(0, str.Length + val); can be replaced with str.Remove(str.Length + val) 可以用str.Remove(str.Length + val)替换str.Remove(str.Length + val)

You can't override a method on string in the strict sense using extension methods, as the compiler will always choose an instance method over an extension method with the same signature when compiling a method call. 您不能使用扩展方法在严格意义上覆盖字符串上的方法,因为在编译方法调用时,编译器将始终在具有相同签名的扩展方法上选择实例方法。 However, you can achieve something close to what you want using named arguments. 但是,您可以使用命名参数实现接近您想要的功能。 This should also help avoid readability issues. 这也应该有助于避免可读性问题。 Here's an example 这是一个例子

public static string Substring(this string @this, int trimFromEnd)
{
    return @this.Substring(0, @this.Length - trimFromEnd);
}

// if you do
"abc".Substring(1) -> returns "bc"

// if you do
"abc".Substring(trimFromEnd: 1) -> returns "ab"

Personally, I find this a bit more readable than Substring(-1) or just Substring(varName), where varName happens to be negative. 就个人而言,我发现这比Substring(-1)或者只是Substring(varName)更具可读性,其中varName恰好是负数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM