我应该为这个扩展方法命名什么?

发布于 2024-08-10 01:31:07 字数 1562 浏览 8 评论 0原文

我编写了一个用于字符串操作的扩展方法。我很困惑应该给它命名什么 - 因为这将成为团队中前端开发人员将使用的基础库的一部分。这是班级成员的简介。

信息:字符串类型的实用扩展方法。此方法的重载可能会对除空格之外的字符执行相同的操作[使用参数中提供的内容]
用途:将所有中间或中间的空格修剪为单个空格。
例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

不气馁 2024-08-17 01:31:07

CollapseSpaces 怎么样?

What about CollapseSpaces?

没有心的人 2024-08-17 01:31:07

CollapseSpaces 仅适用于空格,但为了允许重载,您可能需要 CollapseDelimitersCollapseWhitespace(如果它真的只是用于各种空白)人物。

CollapseSpaces is good for just spaces, but to allow for the overloads you might want CollapseDelimiters or CollapseWhitespace if it's really just going to be for various whitespace characters.

三月梨花 2024-08-17 01:31:07

并不是真正的答案,更多的是对您发布的代码的评论...

您可以通过使用正则表达式使该方法更短且更易于理解。 (我的猜测是,它的性能可能也比递归字符串操作更好,但您需要进行基准测试才能确定。)

public static class StringExtensions
{
    public static string Collapse(this string str)
    {
        return str.Collapse(' ');
    }

    public static string Collapse(this string str, char delimiter)
    {
        str = str.Trim(delimiter);

        string delim = delimiter.ToString();
        return Regex.Replace(str, Regex.Escape(delim) + "{2,}", delim);
    }
}

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.)

public static class StringExtensions
{
    public static string Collapse(this string str)
    {
        return str.Collapse(' ');
    }

    public static string Collapse(this string str, char delimiter)
    {
        str = str.Trim(delimiter);

        string delim = delimiter.ToString();
        return Regex.Replace(str, Regex.Escape(delim) + "{2,}", delim);
    }
}
但可醉心 2024-08-17 01:31:07

在 ruby​​ 中,我相信他们称之为 squeeze< /a>

In ruby I believe they call this squeeze

我很OK 2024-08-17 01:31:07

规范化空白?
这种方式更明确的是,处理后会有可用的价值。
正如其他人之前所说,“折叠”听起来有些严格,甚至可能意味着它可以返回一个空字符串。

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.

半衬遮猫 2024-08-17 01:31:07

试试这个,它对我有用,并且似乎比递归解决方案复杂得多......

public static class StringExtensions
{
    public static string NormalizeWhitespace(this string input, char delim)
    {
        return System.Text.RegularExpressions.Regex.Replace(input.Trim(delim), "["+delim+"]{2,}", delim.ToString());
    }
}

它可以这样调用:

Console.WriteLine(input.NormalizeWhitespace(' '));

Try this, it works for me and seems to be a lot less complicated than a recursive solution...

public static class StringExtensions
{
    public static string NormalizeWhitespace(this string input, char delim)
    {
        return System.Text.RegularExpressions.Regex.Replace(input.Trim(delim), "["+delim+"]{2,}", delim.ToString());
    }
}

It can be called as such:

Console.WriteLine(input.NormalizeWhitespace(' '));
南城追梦 2024-08-17 01:31:07

折叠额外空白

CollapseExtraWhitespace

送你一个梦 2024-08-17 01:31:07

PaulaIsBrilliant 当然!

PaulaIsBrilliant of course!

嘿看小鸭子会跑 2024-08-17 01:31:07

makeCompact 怎么样?

How is makeCompact?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文