java知识点——String类常用方法

字符串常用方法:

 

方法

描述

int 字符串.length()

获取字符串长度

boolean 字符串.equals

比较字符串内容是否相等

boolean 字符串1.equalsIgnoreCase(字符串2)

不分大小写比较内容

String 字符串.toLowerCase()

将字符串全部转成小写的

String 字符串.toUpperCase()

将字符串转成大写的

String 字符串.concat("xxx")

字符串拼接

int 字符串.indexOf("xxx")

从头向后找xxx的下标(这里的xxx可以是字符也可以是字符串,字符串只会出现第一个的下标)

int 字符串.lastIndexOf("xxx")

从后向前找查询某个指定字符是否存在,存在就返回下标

String 字符串.trim()

去除左右空格

String str1.substring(2)

截取字符串,默认到最后

String str1.substring(2,5)

截取从2-5的字符串

boolean str3.endsWith("xxx")

判断字符串是否以指定字符串结尾

char content1.charAt(1)

根据下标获得单个字符

String【】 arr.split("xx")

将一个字符串按指定分隔符分割成多个子字符串,并将这些子字符串存储在一个数组中。

String 字符串.replace("蛋蛋怪","xxx");

将蛋蛋怪替换成xxx,这里用的是字符串替换

String chen.replaceAll("\\d","*");

将所有数字替换成*,replaceAll里面是用的正则表达式,可以用于替换复杂的

代码实例:

  public static void main(String[] args) {
        String str1 = "ASapByBY";
        String str2 = new String("asapbaby");
        //这样它俩就不是同一个地址了,str1在静态常量池中
        int length = str1.length();
        System.out.println("字符串的长度为:"+length);
        boolean equals1 = str1.equals(str2);
        System.out.println("字符相等吗"+equals1);

        System.out.println("不区分大小写比较"+str1.equalsIgnoreCase(str2));
        System.out.println("将字符串转成小写的"+str1.toLowerCase());
        System.out.println("将字符串转成大写的:"+str1.toUpperCase());

        //这样只是拼接了,没有重新赋值给str1
        System.out.println("字符串拼接concat:"+str1.concat("陈梦雨"));


        int a = str1.indexOf("ab");
        System.out.println("从左向右找,查询某个指定字符是否存在,存在就返回下标"+a);

        int b = str1.lastIndexOf("a");
        System.out.println("从右向左找,查询某个指定字符是否存在,存在就返回下标"+b);


        String msg = "  我爱我的祖国   ";

        System.out.println("去除左右空格"+msg.trim());


        System.out.println("截取字符串,默认到最后"+str1.substring(2));

        System.out.println("截取字符串"+str1.substring(2,5));

        System.out.println("————————————————————————————————————————");

        String str3 = "1.png";
        boolean bool = str3.endsWith("png");
        System.out.println("判断字符串是否以指定字符串结尾"+bool);
        System.out.println("————————————————————————————————————————");

        //charAt()函数根据下标获得单个字符;
        //通过for循环遍历和charAt()来获得出现次数
        String content1 = "我爱你中国,中国我爱你";
        System.out.println("根据下标获得单个字符"+content1.charAt(1));
        int count = 0;
        for (int i=0;i<content1.length();i++){
            //char类型的比较通过==来比较
            if (content1.charAt(i)=='爱'){
                count++;

            }
        }
        System.out.println("count爱的出现次数"+count);

        System.out.println("————————————————————————————————————————");

        String arr = "1001+1002,1003";
        //分割字符串,返回数组
        String arrs[] = arr.split("\\+");
        System.out.println("打印分割后的字符串返回的数组"+ Arrays.toString(arrs));

        System.out.println("————————————————————————————————————————");
        //replace用字符串或字符替换
        String chen = "1勇敢的蛋蛋怪77556";

        String chen1= chen.replace("蛋蛋怪","***");
        System.out.println("*替换后的chen字符串:"+chen1);

        chen = chen.replaceAll("\\d","*");
        System.out.println("全部替换后的:"+chen);

        //replace: 用于简单的字符或子字符串替换,不使用正则表达式。
        //replaceAll: 用于根据正则表达式模式进行替换,可以处理复杂的替换规则。



    }

replace和replaceAll:
//replace用字符串或字符替换
//replaceAll用正则表达式替换
        String chen = "1勇敢的蛋蛋怪77556";

        String chen1= chen.replace("蛋蛋怪","***");
        System.out.println("*替换后的chen字符串:"+chen1);
//替换了所有的数字
        chen = chen.replaceAll("\\d","*");
        System.out.println("全部替换后的:"+chen);

练习题:判断邮箱格式是否正确

1.必须包含@和.

2.@要在.之前

3.@和.之间必须存在一个以上字符

4.@之前最少要有2个字符

5.以com,cn,net结尾

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入邮箱");
        String  strEmail = scanner.next();
        int i = strEmail.indexOf("@");
        int i1 = strEmail.indexOf(".");

                if (i >= 0 && i1 >= 0 && i >= 2 && i1 > i + 1 &&
                        (strEmail.endsWith("com") || strEmail.endsWith("cn") || strEmail.endsWith("net"))) {
                    System.out.println("该邮箱格式正确");
                } else {
                System.out.println("该邮箱格式错误");
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值