zoukankan      html  css  js  c++  java
  • Java判断一个字符串中包含另一字符串

    1、contains方法

    1、描述
    java.lang.String.contains() 方法返回true,当且仅当此字符串包含指定的char值序列
    
    2、声明
    public boolean contains(CharSequence s)  
    
    3、返回值
    如果此字符串包含,此方法返回true,否则返回false。
    
    4、案例
    public static void main(String[] args) {  
            String str = "abc";  
            boolean status = str.contains("a");  
            if(status){  
                System.out.println("包含");  
            }else{  
                System.out.println("不包含");  
            }  
        }  

    2、indexOf方法

    1、描述
    java.lang.String.indexOf() 的用途是在一个字符串中寻找一个字的位置,同时也可以判断一个字符串中是否包含某个字符。
    
    2、声明
    int indexOf(int ch,int fromIndex)  
    
    3、返回值
    indexOf的返回值为int
    
    4、案例
    public static void main(String[] args) {  
        String str1 = "abcdefg";  
        int result1 = str1.indexOf("a");  
        if(result1 != -1){  
            System.out.println("字符串str中包含子串“a”"+result1);  
        }else{  
            System.out.println("字符串str中不包含子串“a”"+result1);  
        }  
    } 
  • 相关阅读:
    转: 尽己力,无愧于心 存储过程
    转: 尽己力,无愧于心 FastReport.Net 常用功能总汇
    关于Ansi_Nulls、Quoted_Identifier、Ansi_Padding的用法
    数学建模
    数学建模
    深度学习
    C#复习
    Keras的两种网络模型
    xaml转义
    C# 文件目录操作
  • 原文地址:https://www.cnblogs.com/svipero/p/12841458.html
Copyright © 2011-2022 走看看