【问题标题】:C# - How can i color string if they start and end with the same numberC# - 如果字符串以相同的数字开头和结尾,我如何为字符串着色
【发布时间】:2021-04-30 04:20:27
【问题描述】:

如上所述,如果它们以相同的数字开头和结尾,我需要将字符串的一部分着色。 我是初学者,无法真正弄清楚,我已经用谷歌搜索但没有找到任何答案。

编辑 为了澄清,我需要打印整个字符串,而不仅仅是彩色部分。

string userInput = "29535123p48723487597645723645";
            //Console.WriteLine(userInput);
            
            for (int i = 0; i < userInput.Length; i++)
            {
                if (Char.IsDigit(userInput[i]))
                {
                    for (int j = i + 1; j < userInput.Length; j++)
                    {
                        if (userInput[i] == userInput[j])
                        {
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine(userInput.Substring(i, j - i + 1));
                            break;
                        }
                        else if (!Char.IsDigit(userInput[j]))
                        {

                            break;
                        }
                    }
                }

            }
            Console.ReadKey();

【问题讨论】:

  • 如何定义“以相同的数字开头和结尾”?能举个例子吗?
  • 哪个部分必须上色?要遵循什么规则?
  • 你的意思是像“29abc29”然后“29”应该是绿色的吗?
  • 例如,如果字符串包含“2432a302”,则绿色部分为“2432”,但“a302”为白色对于我使用的字符串“29535123p48723487597645723645”,绿色位将为“ 2953512”“535”“35123”“487234”“872348”等等。我遇到的问题是我编写的代码只打印出彩色位,而我需要整个字符串。
  • 这对我来说完全没有意义

标签: c# string numbers substring


【解决方案1】:

我试了一下。我将尝试在 cmets 中进行解释:

class Program
{
    // input given in Question by OP
    public static string testinput = "29535123p48723487597645723645";

    static void Main(string[] args)
    {
        ProcessString(testinput);
        Console.ReadKey();
    }

    private static void ProcessString(string input)
    {
        // We'll have one index running from start of the string
        for (int fromStart = 0; fromStart < input.Length - 1; fromStart++)
        {
        // and another one running from the end
            for (int fromEnd = input.Length - 1; fromEnd > fromStart; fromEnd--)
            {
                // If both match, check if the found substring is all-digits
                if (input[fromStart] == input[fromEnd])
                {
                    if( input.Substring(fromStart, fromEnd-fromStart).IsNumCharOnly() )
                        PrintResult(testinput, fromStart, fromEnd);
                }
            }
        }
    }

    public static void PrintResult(string original, int fromIndex, int toIndex)
    {
        // remember the original fg-color
        var originalColor = Console.ForegroundColor;
        // Print the part before the hit
        Console.Write(original.Substring(0, fromIndex));
        // change color
        Console.ForegroundColor = ConsoleColor.Green;
        // print the hit
        Console.Write(original.Substring(fromIndex, toIndex-fromIndex +1));
        // change color back
        Console.ForegroundColor = originalColor;
        // print the rest, if there is something left
        if( toIndex < original.Length-1)
            Console.Write(original.Substring(toIndex+1));
        // add a newline in any case.
        Console.WriteLine();
    }
}

static class StringExtensions
{
    // Extensionmethod on string
    public static bool IsNumCharOnly(this string str)
    {
        // if any of the string's chars is outside '0'-'9' chars, 
        // return false, otherwise return true
        return !str.Any(c => c < '0' || c > '9');
    }
}

现在,它有什么作用?

我们从头到尾遍历字符串,直到找到匹配项。 然后我们检查由 to 索引分隔的字符串是否包含非数字。 如果是这样,我们忽略候选人,否则我们有一个获胜者并打印它。

29535123p48723487597645723645 ^ ^ = 没有 ^ ^ = 没有 ^ ^ = 没有 ^ ^ = 没有 ^ ^ = 是的! => 候选:29535123p4872348759764572 包含“p” => meh 29535123p48723487597645723645 ^ ^ = 没有 ... // 更多误报,我们忽略 ... 29535123p48723487597645723645 ^ ^ = 是的! => 候选人:2953512 => 打印! ... 29535123p48723487597645723645 ^^ = 没有 // 下一个 OUTER 循环迭代: 29535123p48723487597645723645 ^ ^ = 没有 ... 等等

输出:


编辑:C# 8

通过访问"Range" 表示法,它甚至更易读:

if( input[fromStart..fromEnd].IsNumCharOnly() )

【讨论】:

  • 哇.. 太棒了,非常感谢。我测试了代码,它运行良好,正是我想要的。
  • @Arcee 不过,请确保您理解它。有些部分,你可以玩弄。例如:在字符串扩展中,我可以使用Char.IsDigit。我认为要带走的一件事是:单独的关注点。找到你的结果 => Algo 的部分。打印结果 => 不同的页面。将两者分开,我可以轻松更改结果的显示方式。而且我可以在不破坏显示的情况下更改算法...
  • 是的,我注意到了 Char.IsDigit 的东西,我将保存代码并尝试更改一些东西来学习和理解。再次感谢
  • 我不明白的是扩展方法,你介意澄清一下吗?
  • 非常感谢! =)
【解决方案2】:

也许是这样的?

        String s1 = "1234567890";
        String s2 = "1234567891";

        changeColor(s1);
        changeColor(s2);


        
        void changeColor(String s)
        {
            char start = s[0];
            char end = s[s.Length-1];

            if (start == end)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write(s[0]);
                Console.ForegroundColor = ConsoleColor.White;
                for (int i = 1; i < s.Length - 2; i++)
                {
                    Console.Write(s[i]);
                }
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write(s[s.Length-1]);
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine(s);
            }
        }

【讨论】:

    猜你喜欢
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多