S
S
stas09092021-10-28 11:01:22
Java
stas0909, 2021-10-28 11:01:22

How to find the fragment of an array element in Java?

I have an ArrayList, I add rows to it in a loop, all the rows are different, but in some the first and last names are the same. i need to not write the same ones, i use array.contains(string) but it doesn't work, it just skips all the records, what am i doing wrong?

This is what the sheet looks like:

array [0] = 1|Иванов|Иван|sdhfj|fjdkfhjdf|sdsd|;
array[1] = 2|Петров|Петро|sdsd|sdsd|sdfsd|;

and this is the line to be checked:
String string = "Петров|Петро";

this is how I thought it would work:

if (!array.contains(string)){
int i = i +1;
   array.add(i + "|" + string + string2);}


but not working(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Constable, 2021-10-30
@Constable

array.contains will only find string references that already exist (full matches).
From the statement of the problem it follows that it is necessary to find a substring.
To do this, you need to use String.contains in a loop, or in a Stream:

if (array.stream().filter(s -> s.contains(string)).count() > 0)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question