0

我想向字符串添加一个方法,将空格字符转换为下划线(扩展方法),我部署了代码,但为什么它不起作用?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = "moslem";
            string.SpaceToUnderScore(name);
        }

        public static string SpaceToUnderScore(this string source)
        {
            string result = null;
            char[] cArray = source.ToArray();
            foreach (char c in cArray)
            {
                if (char.IsWhiteSpace(c))
                {
                    result += "_";
                }
                else
                {
                    result += c;
                }
            }
            return result;
        }
    }
}

为什么它不起作用?

4

4 回答 4

3

首先将您的扩展方法放入静态类,然后调用为name.SpaceToUnderScore()

var newstr = "a string".SpaceToUnderScore();


public static class SomeExtensions
{
    public static string SpaceToUnderScore(this string source)
    {
        return new string(source.Select(c => char.IsWhiteSpace(c) ? '_' : c).ToArray());
        //or
        //return String.Join("",source.Select(c => char.IsWhiteSpace(c) ? '_' : c));
    }
}
于 2012-10-07T16:23:34.180 回答
0

扩展方法应该写在静态类中。

例子

public static class StringExtension
{
    public static string SpaceToUnderScore(this string source)
    {
        string result = null;
        char[] cArray = source.ToArray();
        foreach (char c in cArray)
        {
            if (char.IsWhiteSpace(c))
            {
                result += "_";
            }
            else
            {
                result += c;
            }
        }
        return result;
    }
}
于 2012-10-07T16:18:12.233 回答
0

在不更改扩展方法中的代码的情况下,将扩展方法添加到静态类:

public static class MyExtensions // Name this class to whatever you want, but make sure it's static.
{
    public static string SpaceToUnderScore(this string source)
    {
        string result = null;
        char[] cArray = source.ToArray();
        foreach (char c in cArray)
        {
            if (char.IsWhiteSpace(c))
            {
                result += "_";
            }
            else
            {
                result += c;
            }
        }
        return result;
    }
}

然后像这样调用它:

string name = "moslem";
string underscoreName = name.SpaceToUnderScore(); // Omit the name parameter (prefixed with this) when called like this on the string instance. 

// This would work to:
string underscoreName = MyExtentions.SpaceToUnderScore(name);

如果您没有找到扩展方法,请确保您使用的是静态类的命名空间。

于 2012-10-07T16:21:30.080 回答
0
public static class MyStringExtenstionClass
{

    public static string SpaceToUnderScore(this string source)
    {
        string result = null;
        char[] cArray = source.ToArray();
        foreach (char c in cArray)
        {
            if (char.IsWhiteSpace(c))
            {
                result += "_";
            }
            else
            {
                result += c;
            }
        }
        return result;
    }
}

像这样

 string name = "John Doe";
 name = name.SpaceToUnderScore(); //no argument and called on instance of string
于 2012-10-07T16:21:52.063 回答