Compare First 3 Character String with List of String of Each String First 3 Character

后端 未结 5 1258

The Sample Code which needs a solution?

public class TestJJava {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

               


        
5条回答
  •  既然无缘
    2021-01-26 07:47

    You are not comparing the full String in the list but rather a substring of one of strings in the list.

    You will have to loop through the list and check with each String individually.

    String temp = abc.substring(0,3);
    boolean flag = true;
    for(String value: lstValues.subList(0, 3))
     if(value.contains(temp)) // or if(value.indexOf(temp) != -1)
      {
        System.out.println("**** Match Found ****");
        flag = false;
        break;
      }
    if(flag)
      System.out.println("**** No Match Found ****");
    

提交回复
热议问题