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));



    }
}

 

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

被迫内卷的学习记录

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值