Answers for "check if string in array java"

6

Check if an array contains an element java

import java.util.Arrays;

// For String
String[] array = {"Boto", "Nesto", "Lepta"};
String toSearch = "Nesto";

// Inline
if (Arrays.toString(array).contains(toSearch)) {
	// Do something if it's found
}

// Multi line
String strArray = Array.toString(array);
if (strArray.contains(toSearch)) {
	// Do your thing
}

// Different elements
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int number = 5;

String checker = Arrays.toString(numbers);

// The toString of int in some cases can happen without explicitly saying so
// In this example we convert both
if (checker.contains(Integer.toString(number)) {
	// Found.     
}
Posted by: Guest on January-14-2021
1

java if one sting on array match

String[] fieldsToInclude = { "id", "name", "location" };

if ( ArrayUtils.contains( fieldsToInclude, "id" ) ) {
    // Do some stuff.
}
Posted by: Guest on February-13-2021
2

java array contains

Arrays.asList(yourArray).contains(yourValue)
Posted by: Guest on September-19-2020
-2

array contains java

StringArrayExample1.java

package com.mkyong.core;

import java.util.Arrays;
import java.util.List;

public class StringArrayExample1 {

    public static void main(String[] args) {

        String[] alphabet = new String[]{"A", "B", "C"};

        // Convert String Array to List
        List<String> list = Arrays.asList(alphabet);
        
        if(list.contains("A")){
            System.out.println("Hello A");
        }

    }

}
Posted by: Guest on February-13-2021
0

arrays .contains methof

private static final Set<String> VALUES = Set.of(
    "AB","BC","CD","AE"
);
Posted by: Guest on October-23-2020

Code answers related to "check if string in array java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language