判断一个字符串是否在一个字符串数组里

判断一个字符串是否在一个字符串数组里

package test;

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

public class isBelongList {
    private static boolean isBelongList(String str){
        String[] strArray = {"1","2","3"};        
        boolean bResult = false;


//      for (String temp : strArray) {
//          if (temp.equals(str)) {
//              bResult = true;
//          }
//      }

        List<String> fauCodeList = new ArrayList<String>();
        fauCodeList=Arrays.asList(strArray);
        if(fauCodeList.contains(str)==true)
        {
            bResult=true;

        }


        return bResult; 
    }
    public static void main(String[] args)
    {
        if(isBelongList("4"))
        {
            System.out.println("yes");
        }else
        {
            System.out.println("no");
        }
    }

}

将字符串输入插入函数

package test;

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

public class isBelongList {
    private static boolean isBelongList(String[] strArray,String str){

        boolean bResult = false;


//      for (String temp : strArray) {
//          if (temp.equals(str)) {
//              bResult = true;
//          }
//      }

        List<String> fauCodeList = new ArrayList<String>();
        fauCodeList=Arrays.asList(strArray);
        if(fauCodeList.contains(str)==true)
        {
            bResult=true;

        }


        return bResult; 
    }
    public static void main(String[] args)
    {
        String[] strArray = new String[200];  
        strArray[0]="1";
        strArray[1]="2";
        if(isBelongList(strArray,"2"))
        {
            System.out.println("yes");
        }else
        {
            System.out.println("no");
        }
    }

}
在 JavaScript 中,判断一个字符串是否数组有多种方法,以下为你详细介绍: #### 使用 `indexOf()` 方法 `indexOf()` 方法用于查找指定元素在数组中的第一个匹配位置,如果未找到则返回 -1。示例代码如下: ```javascript const number = ["A", "B", "AB", "ABC"]; const a = number.indexOf("AB"); if (a > -1) { console.log("表示数组中含有此字符串"); } else { console.log("不含有此字符串,a的值为-1"); } ``` #### 使用 `find()` 方法 `find()` 方法会返回数组中满足提供的测试函数的第一个元素的值,如果没有找到合适元素,则返回 `undefined`。示例代码如下: ```javascript const arr = ["apple", "banana", "cherry"]; const found = arr.find((value) => value === "banana"); if (found) { console.log("表示数组中含有此字符串"); } else { console.log("不含有此字符串"); } ``` #### 使用 `findIndex()` 方法 `findIndex()` 方法返回数组中满足提供的测试函数的第一个元素的索引,如果没有找到则返回 -1。示例代码如下: ```javascript const array = ["dog", "cat", "bird"]; const index = array.findIndex((element) => element === "cat"); if (index > -1) { console.log("表示数组中含有此字符串"); } else { console.log("不含有此字符串,index的值为-1"); } ``` #### 使用 `some()` 方法 `some()` 方法测试数组是否至少有一个元素通过了由提供的函数实现的测试,如果有一个元素满足条件,则返回 `true`,否则返回 `false`。示例代码如下: ```javascript const array = ['a', 'b', 'c', 'd']; const char = 'b'; if (array.some((element) => element === char)) { console.log(`${char} 存在于数组中`); } else { console.log(`${char} 不存在于数组中`); } ``` #### 使用 `Set` 可以将数组转换为 `Set`,然后使用 `has()` 方法来检查字符串是否存在于集合中。示例代码如下: ```javascript const array = ["hello", "world"]; const set = new Set(array); if (set.has("hello")) { console.log("表示数组中含有此字符串"); } else { console.log("不含有此字符串"); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值