C#程序计算多少个字符串具有与列表中相同的首字符和尾字符

问题描述 投票:-5回答:5

我需要编写一个程序来计算字符串长度为2或更大,并且第一个和最后一个字符与某些带有字符串的列表中的字符串相同的字符串数[“人物”,“办公桌”,“橙色”,“黄色”,“胡萝卜”,“菠萝”]

示例:输入:“ pe”,结果-2-与“人民”和“菠萝”相同]

现在我这样做了,它搜索第一个和最后一个字符是否相同:

        List<string> stringsList = new List<string> { "people", "desk", "orange", "yellow", "carrot", "pineapple"};

        int count = 0;

        foreach (var c in stringsList)
        {
            if (c.Length > 1 && c[0] == c[-1])
            {
                count += 1;
            }
        }
        Console.WriteLine(count);
c# string list
5个回答
0
投票

您可以为此目的使用linq。所以你需要

  • 按长度筛选
  • 将第一个字母与输入进行比较
  • 将最后一个字母与输入进行比较

所以代码看起来像这样:

List<string> stringsList = new List<string> { "people", "desk", "orange", 
    "yellow", "carrot", "pineapple" };

int count = 0;
var input = "pe";
count = stringsList.Where(s => s.Length > 2 
            &&  s[0] == input[0] 
            && s[s.Length - 1] == input[1])
            .Count();

0
投票

尝试下面的答案:

   //I'll let you input however you want to. Probably use console.ReadLine();
    string input = Console.ReadLine();
    int count;
    List<string> stringsList = new List<string> { "people", "desk", "orange", "yellow", "carrot", "pineapple"};

            foreach (string item in stringsList)
            {
                if (input.Length > 1 && item.Length > 1)
                {
                    if(input[input.Length - 1] == item[item.Length - 1])
                    {
                        //Same Last character
                        if(input[0] == item[0])
                        {
                            //Same first character
                            count += 1;
                        }
                    }
                }
            }
            Console.WriteLine(count);

0
投票

您可以在Linq的帮助下进行查询:

using System.Linq;

...

List<string> stringsList = new List<string> { 
  "people", "desk", "orange", "yellow", "carrot", "pineapple"
};

string input = "pe";

int count = stringsList
  .Count(word => word.Length >= 2 && 
                 word[0] == input[0] && 
                 word[word.Length - 1] == input[input.Length - 1]);

甚至

int count = stringsList
  .Count(word => word.Length >= 2 && 
                 word.First() == input.First() && 
                 word.Last() == input.Last());

0
投票

如果使用的是C#8和.Net Core 3,则可以使用新的“ end from end”运算符获取字符串的最后一个字符,从而在某种程度上简化代码。

例如,字符串str的最后一个字符由str[^1]给出。

因此,您可以使用Linq Count()运算符计算所有长度大于等于2的字符串,其中第一个和最后一个字符与目标匹配;例如:

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

namespace CoreConsole
{
    public static class Program
    {
        static void Main()
        {
            List<string> stringsList = new List<string> { "people", "desk", "orange", "yellow", "carrot", "pineapple" };

            string search = "pe";

            int n = stringsList.Count(str => 
                str.Length >= 2
                && str[ 0] == search[0] 
                && str[^1] == search[1]);

            Console.WriteLine(n);
        }
    }
}

0
投票

谢谢,这就是我所做的:

        foreach (var c in stringsList)
        {
            if (c.Length > 1 && c[0] == input[0] && c[c.Length-1] == input[1])
            {
                count += 1;
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.