String

此博客展示了Java中字符串的常用方法,包括判断字符串是否相等、转换为字符数组和字节数组、查找字符、判断包含关系、判断前后缀、截取子字符串以及获取字符串长度等,并给出了相应的代码示例。

package cn.lijun.demo2;

public class StringDemo3 {
    public static void main(String[] args) {
        fun9();
    }
    //9  boolean  equals(Object obj);  判断字符串里面完全相等 返回true
    //s.equalsIgnoreCase(s1)  不区分大小写的比较
    public static void fun9(){
        String s = "hello";
        String s1= "hEllo";
        System.out.println(s.equals(s1));
        System.out.println(s.equalsIgnoreCase(s1));
        
    }
    //8 将字符串  转字符数组
    public static void fun8(){
        String s = "今晚11点老地方见 不见不散";
        char[] s1 = s.toCharArray();
        for (int i = 0; i < s1.length; i++) {
            System.out.println(s1[i]);
        }
        
    }
    //7 将字符串转字节数组  getBytes();
    
    public static void fun7(){
        String s = "今晚11点老地方见 不见不散";
        byte[] s1 = s.getBytes();
        //System.out.println(s1);
        for (int i = 0; i < s1.length; i++) {
            System.out.println(s1[i]);
        }
        
    }
    // 6 查找一个字符  indexOf(char ch)  返回int   返回-1 没有找到   
    public static void fun6(){
        String s = "hello.java";
        int s1 = s.indexOf('w');
        System.out.println(s1);
        
    }
    //5  contains判断一个字符串是否有另一个字符串
    
    public static void fun5(){
        String s = "hello.java";
        boolean s1 = s.contains("ll");
        System.out.println(s1);
    }
    //4判断一个字符串的后缀 结尾   endsWith("参数");
    public static void fun4(){
        String s = "hello.java";
        boolean s1 = s.endsWith(".java");
        System.out.println(s1);
    }
    //3boolean statsWith(String preFix)  判断一个字符串是否包含另一个字符串
    public static void fun3(){
        String s = "helloworld";
        boolean s1 = s.startsWith("hello");
        System.out.println(s1);
    }
    //2 substring(int beginIndex,int endIndex)获取字符串的一部分 包含头 不包含尾
    //substring(int beginIndex)  后面的全要
    
    public static void fun2(){
        String s = "helloworld";
        String s2 = s.substring(2);
        //String s1 = s.substring(1, 4);
        System.out.println(s2);
    }
    // 1 int length();  返回的是字符串的长度
    public static void fun1(){
        String string = "skjl";
        int l = string.length();
        System.out.println(l);
    }
}

转载于:https://www.cnblogs.com/hsx1996/p/10556405.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值