将字符串中的每个空格替换为空格数

时间:2018-02-23 08:03:51

标签: c# regex

我有以下挑战。我需要用一个数字替换字符串中的每个空格,该数字表示有多少空格。 例如:“Hello World!”;应修改为“Hello1World2!”;  我如何在c#中实现这一目标。请帮忙

7 个答案:

答案 0 :(得分:5)

我终于有了一个正则表达式的答案: 使用MatchEvaluator - 正则表达式:

using System.Text.RegularExpressions;

static void Main(string[] args)
{

    string input = "bla absl ael   dls ale ";

    var result2 = Regex.Replace(input, "\s+", new MatchEvaluator(ReplaceByCount));
    Console.WriteLine(result2);
    //returns bla1absl1ael3dls1ale1
}

private static string ReplaceByCount(Match m)
{
    return m.Value.Length.ToString();
}

编辑:已更换" +"用" \ s +"在@ Marco-Forbergs评论之后匹配所有空格字符

答案 1 :(得分:1)

我希望这适合你:

string testString = "Hello World  !   ";
StringBuilder resultBuilder = new StringBuilder();
int charCount = 0;

foreach(char character in testString)
{
    if(Char.IsWhiteSpace(character))
    {
        charCount++;
    }
    else
    {
        if(charCount > 0)
        {
            resultBuilder.Append(charCount);
            charCount = 0;
        }
        resultBuilder.Append(character);
    }
}

if (charCount > 0)
{
    resultBuilder.Append(charCount);
    charCount = 0;
}

testString = resultBuilder.ToString();

答案 2 :(得分:0)

        //your base string value
        string baseString = "Hello World !";

        for (int i = 0; i < baseString.Length; i++)
        {
            //check if have empty sumbol by position in string
            if (baseString[i] == ' ')
            {
                //replace symbol logic
                System.Text.StringBuilder sb = new System.Text.StringBuilder(baseString);
                //here you can add logic for random number
                sb[i] = '1';
                baseString = sb.ToString();
            }
        }

答案 3 :(得分:0)

是空格的数量(&#39;&#39; = 1,&#39;&#39; = 3(连续3个空格......)?)第一个空格是1第二个是2等?

如果是&#34;索引&#34; - 每个空格的数字,请尝试:

private void SpaceNumber()
{
    StringBuilder sb = new StringBuilder();
    int sc = 1;
    string input = "bla absldf el kdslf ael dls ale xyz";

    foreach (string x in input.Split(' '))
    {
        sb.Append(x);
        sb.Append(sc);
        sc++;
    }

    sb.Remove(sb.Length - 1, 1);

    string output = sb.ToString();
}

答案 4 :(得分:0)

这应该可以帮助你。 我没有使用开发工具,所以请将其视为伪代码:

string testString = "Hello World !";
int spaceCounter = 0;
for (int i = 0; i<testString.length; i++)
{
    if (testString.charAt(i) == " ")
    {
        spaceCounter++;
        testString.CharAt(i) = spaceCounter.toString();
    }
}

答案 5 :(得分:0)

很好的答案!

现在轮到我了:

        string theWords = "Hello World of C Sharp!";
        int i = 1;

        int pos = theWords.IndexOf(' ');
        while (pos >= 0)
        {
            theWords = theWords.Substring(0, pos) + i.ToString() + theWords.Substring(pos + 1);
            pos = theWords.IndexOf(' ');
            i++;
        }

答案 6 :(得分:0)

这是一个LINQ解决方案:

string str = "Hello World !";

string result = str.Split()
                .Zip(Enumerable.Range(1, str.Split().Length), (x, y) => x + y)
                .Aggregate((x, y) => x + y).Remove(str.Length);