小编use*_*486的帖子

在C#中使用LINQ在字符串数组中查找确切的子字符串

我正在尝试查看字符串数组中是否存在确切的子字符串.如果子字符串存在于字符串中,它将返回true,但它将包含拼写错误.

编辑:例如,如果我正在检查Connecticut字符串数组中是否存在' 但它拼写' Connecticute'它仍将返回true,但我不希望它.我希望它为'Connecticute'返回false并且仅为'Connecticut'返回true

有没有办法用LINQ做到这一点?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication2
{
class Program
{
    static void Main(string[] args)
    {
        string[] sample = File.ReadAllLines(@"C:\samplefile.txt");
        /* Sample file containing data organised like
        Niall      Gleeson      123 Fake Street     UNIT 63     Connecticute     00703       USA      
         */

        string[] states = File.ReadAllLines(@"C:\states.txt"); //Text file containing list of all US states
        foreach (string s in sample)
        {
            if (states.Any(s.Contains))
            {
                Console.WriteLine("Found State");
                Console.WriteLine(s);
                Console.ReadLine();
            }
            else …
Run Code Online (Sandbox Code Playgroud)

c# linq arrays string substring

3
推荐指数
1
解决办法
760
查看次数

标签 统计

arrays ×1

c# ×1

linq ×1

string ×1

substring ×1