我应该为这个扩展方法命名什么?
我编写了一个用于字符串操作的扩展方法。我很困惑应该给它命名什么 - 因为这将成为团队中前端开发人员将使用的基础库的一部分。这是班级成员的简介。
信息:字符串类型的实用扩展方法。此方法的重载可能会对除空格之外的字符执行相同的操作[使用参数中提供的内容]
用途:将所有中间或中间的空格修剪为单个空格。
例如:
string Input = "Hello Token1 Token2 Token3 World! ";
string Output = Input.TrimSpacesInBetween();
//Output will be: "Hello Token1 Token2 Token3 World!"
我已经阅读了[事实上我正在阅读]框架设计指南,但这似乎让我感到困扰。
我认为一些选项..
TrimIntermediate();
TrimInbetween();
这是请求的代码:
它是递归的..
public static class StringExtensions
{
public static string Collapse(this string str)
{
return str.Collapse(' ');
}
public static string Collapse(this string str, char delimeter)
{
char[] delimeterts = new char[1];
delimeterts[0] = delimeter;
str = str.Trim(delimeterts);
int indexOfFirstDelimeter = str.IndexOf(delimeter);
int indexTracker = indexOfFirstDelimeter + 1;
while (str[indexTracker] == delimeter)
indexTracker++;
str = str.Remove(indexOfFirstDelimeter + 1, indexTracker - indexOfFirstDelimeter - 1);
string prevStr = str.Substring(0, indexOfFirstDelimeter + 1);
string nextPart = str.Substring(indexOfFirstDelimeter + 1);
if (indexOfFirstDelimeter != -1)
nextPart = str.Substring(indexOfFirstDelimeter + 1).Collapse(delimeter);
string retStr = prevStr + nextPart;
return retStr;
}
}
I have written an extension method for string manipulation. I'm confused what should I name it - since this will become part of the base library front-end developers in the team will use. Here's the profile of the class member.
Info: Utility Extension method for String types. Overloads of this method may do the same thing characters other than space [with what supplied in argument]
Purpose: Trims down all intermediate or in-between spaces to single space.
Ex:
string Input = "Hello Token1 Token2 Token3 World! ";
string Output = Input.TrimSpacesInBetween();
//Output will be: "Hello Token1 Token2 Token3 World!"
I have read [in fact I'm reading] the Framework Design guidelines but this seems to be bothering me.
Some options I think..
TrimIntermediate();
TrimInbetween();
Here's the code on Request:
It's recursive..
public static class StringExtensions
{
public static string Collapse(this string str)
{
return str.Collapse(' ');
}
public static string Collapse(this string str, char delimeter)
{
char[] delimeterts = new char[1];
delimeterts[0] = delimeter;
str = str.Trim(delimeterts);
int indexOfFirstDelimeter = str.IndexOf(delimeter);
int indexTracker = indexOfFirstDelimeter + 1;
while (str[indexTracker] == delimeter)
indexTracker++;
str = str.Remove(indexOfFirstDelimeter + 1, indexTracker - indexOfFirstDelimeter - 1);
string prevStr = str.Substring(0, indexOfFirstDelimeter + 1);
string nextPart = str.Substring(indexOfFirstDelimeter + 1);
if (indexOfFirstDelimeter != -1)
nextPart = str.Substring(indexOfFirstDelimeter + 1).Collapse(delimeter);
string retStr = prevStr + nextPart;
return retStr;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
CollapseSpaces
怎么样?What about
CollapseSpaces
?CollapseSpaces
仅适用于空格,但为了允许重载,您可能需要CollapseDelimiters
或CollapseWhitespace
(如果它真的只是用于各种空白)人物。CollapseSpaces
is good for just spaces, but to allow for the overloads you might wantCollapseDelimiters
orCollapseWhitespace
if it's really just going to be for various whitespace characters.并不是真正的答案,更多的是对您发布的代码的评论...
您可以通过使用正则表达式使该方法更短且更易于理解。 (我的猜测是,它的性能可能也比递归字符串操作更好,但您需要进行基准测试才能确定。)
Not really an answer, more a comment on your posted code...
You could make the method a lot shorter and more understandable by using a regular expression. (My guess is that it would probably perform better than the recursive string manipulations too, but you would need to benchmark to find out for sure.)
在 ruby 中,我相信他们称之为
squeeze
< /a>In ruby I believe they call this
squeeze
规范化空白?
这种方式更明确的是,处理后会有可用的价值。
正如其他人之前所说,“折叠”听起来有些严格,甚至可能意味着它可以返回一个空字符串。
NormalizeWhitespace ?
This way is more clear that there will be a usable value left after processing.
As other have stated earlier, 'Collapse' sounds somewhat rigorous and might even mean that it can return an empty string.
试试这个,它对我有用,并且似乎比递归解决方案复杂得多......
它可以这样调用:
Try this, it works for me and seems to be a lot less complicated than a recursive solution...
It can be called as such:
折叠额外空白
CollapseExtraWhitespace
PaulaIsBrilliant
当然!PaulaIsBrilliant
of course!makeCompact 怎么样?
How is
makeCompact
?