String的判断、获取功能

String的判断、获取功能,第1张

String的判断、获取功能

String类的判断功能

boolean equals(Object obj)
boolean equalsIgnoreCase(String str)
boolean contains(String str)
boolean startsWith(String str)
boolean endsWith(String str)
boolean isEmpty()

 以上功能没什么号讲解的,就直接上代码好了

public class StringDemo6 {
    public static void main(String[] args) {
        String s1 = "helloworld";
        String s2 = "Helloworld";
        String s3 = "HelloWorld";

        //boolean equals(Object obj) 比较字符串的内容是否相同 区分大小写
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println("**************************");
        //boolean equalsIgnoreCase(String str)
        //比较字符串的内容是否相同,忽略大小写
        System.out.println(s1.equalsIgnoreCase(s2));
        System.out.println(s1.equalsIgnoreCase(s3));
        System.out.println("**************************");
        //boolean contains(String str)
        //当且仅当此字符串包含指定的char值序列时才返回true。
        //判断大的字符串中是否包含小的字符串,如果包含,返回true,反之false
        //区分大小写
        System.out.println(s1.contains("Hello"));
        System.out.println(s1.contains("leo"));
        System.out.println(s1.contains("hello"));
        System.out.println("**************************");
        //boolean startsWith(String str)
        //测试此字符串是否以指定的前缀开头。
        //区分大小写
        System.out.println(s1.startsWith("hel"));
        System.out.println(s1.startsWith("h"));
        System.out.println(s1.startsWith("he"));
        System.out.println(s1.startsWith("he34"));
        System.out.println(s1.startsWith("H"));
        System.out.println("**************************");
        //boolean endsWith(String str)
        //测试此字符串是否以指定的后缀结束。
        //区分大小写
        System.out.println(s1.endsWith("orld"));
        System.out.println(s1.endsWith("orlD"));
        System.out.println("**************************");
        //boolean isEmpty() 判断字符串是否是空字符串
        System.out.println(s1.isEmpty());
        System.out.println("**************************");

        String s4 = "";
        String s5 = null;
        System.out.println(s4==s5);
        System.out.println(s4.isEmpty());
        //NullPointerException
//        System.out.println(s5.isEmpty());
        System.out.println("**************************");
        String s6 = "bigdata";
        String s7 = null;
    }
}

以上就是字符串的判断功能了,不过有一点是值得注意到的:符串之间比较的要求,在不知道两个字符串变量的值的时候,为了防止空指针异常,把变量放在后面

public class StringDemo6 {
    public static void main(String[] args) {


//需求:将s6,s7与"hadoop"进行比较
        System.out.println(s6.equals("hadoop"));
        System.out.println(s7.equals("hadoop"));
        
        System.out.println("hadoop".equals(s6));
        System.out.println("hadoop".equals(s7));

  }
}

String类的获取功能

int length()
char charAt(int index)
int indexOf(int ch)
int indexOf(String str)
int indexOf(int ch,int fromIndex)
int indexOf(String str,int fromIndex)
String substring(int start)
String substring(int start,int end)

来,兄弟们,直接上代码!

public class StringDemo7 {
    public static void main(String[] args) {
        String s = "helloworld";

        //int length() 获取字符串的长度
        System.out.println(s.length());
        System.out.println("*************************");

        //char charAt(int index) 返回char字符指定位置索引的值
        //索引从0开始到length()-1
        System.out.println(s.charAt(7));
        System.out.println(s.charAt(0));
        //StringIndexOutOfBoundsException
//        System.out.println(s.charAt(100));

        System.out.println("*************************");
        //int indexOf(int ch)
        //返回指定字符第一次出现的字符串内的索引
        //如果此字符串中没有此类字符,则返回-1 。
        System.out.println(s.indexOf('l'));
        System.out.println(s.indexOf(97));
        System.out.println("*************************");
        //int indexOf(String str)
        //helloworld
        //返回的是字符串第一个字符在大字符串中的索引值
        //如果k的值不存在,则返回-1
        System.out.println(s.indexOf("owo"));
        System.out.println(s.indexOf("qwer"));
        System.out.println("*************************");
        //int indexOf(int ch,int fromIndex)
        //返回指定字符第一次出现在字符串中的索引,以指定的索引开始搜索
        System.out.println(s.indexOf('l',4));//8
        System.out.println(s.indexOf('p',4)); //-1
        System.out.println(s.indexOf('l',40));//-1
        System.out.println(s.indexOf('p',40));//-1

        System.out.println("*************************");
        //int indexOf(String str,int fromIndex)
        System.out.println("*************************");
        //String substring(int start)
        //返回的是一个字符串,该字符串是此字符串的子字符串
        //子字符串从指定的索引处开始,并扩展到字符拆的末尾
        //包含开始索引位置的值
        //helloworld
        System.out.println(s.substring(3)); //loworld
        //.StringIndexOutOfBoundsException
//        System.out.println(s.substring(20));

        System.out.println("*************************");
        //String substring(int start,int end)
        //返回的是一个字符串,该字符串是此字符串的子字符串
        //字串开始于start位置,并截取到end-1的位置
        //左闭右开  [,)  含头不含尾
        System.out.println(s.substring(5,10));//world
        //StringIndexOutOfBoundsException
//        System.out.println(s.substring(1,20));



    }
}

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5676472.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存