Use of "Contains" in search expression

Asked

Viewed 124 times

1

I have a string _Users of the kind List, it contains several lines.

Each line consists of an integer and a string separated by comma (in general it is only one string): ID (numbers only),name (letters and numbers).

What I’m trying to do is find out which line the ID is located on.

If I want to find the index of ID 1, the function Contains() will return me the first line containing "1", it will disturb if there is a user with name, for example: "joao123".

int i = _Users.FindIndex(a => a.Contains(UserID.ToString()));

var dados = _Users[i];

string[] dado = dados.Split(',');
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

3 answers

1

From what I understand of your question, you need to capture exactly the user with the "id" equal to UserID.

This way you don’t need comparison using Contains, it is possible to break the user string by comma using the method Split().

var userId = 1;
var users = new [] { "1,Joao", "2,Marcos", "3,Joaquim123", "31,Jonas" };

// Faça assim para obter o usuário com exatamente o "id" = userId
var usuario = users.First(u => u.Split(',')[0] == userId.ToString());

// Faça isso para obter OS USUÁRIOS onde o "id" CONTENHA userId
var usuarios = users.Where(u => u.Split(',')[0].Contains(userId.ToString()));

Console.WriteLine($"Usuário com Id = {userId}");
Console.WriteLine($"\t{usuario}");

Console.WriteLine($"\n\nUsuário onde o Id contém {userId}");
foreach(var u in usuarios)
    Console.WriteLine($"\t{u}");

See working on . NET Fiddle

1

Use LINQ. I don’t know if you want to take the id or the nome, did both:

using static System.Console;
using System.Collections.Generic;
using System.Linq;

public class Program {
    public static void Main() {
        var lista = new List<string> { "1,Joao",
            "2,Maria",
            "3,José1" };
        var nome = lista.Select(x => x.Split(',')).SingleOrDefault(a => a[1].Contains("1"));
        var id = lista.Select(x => x.Split(',')).SingleOrDefault(a => a[0].Contains("1"));
        WriteLine(nome[1]);
        WriteLine(id[1]);
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

You need to first isolate what’s making noise, then the Split() has to occur before, and the search should occur only in the name, then it returns the item already split.

0

It would be easier if you just write one of these lines as an example.

If she looks like 1, Funaldo de Tal, you could use StartsWith instead of Contains, so if the ID is not found early on, the comparison will return false.

int i = _Users.FindIndex(a => a.StartsWith(UserID.ToString()));

  • In this your solution with StartsWith(UserID) has a hole, because, if UserID == 1, for example, your solution could return a UserID == 10, if the list is not sorted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.