Java之Sring(系统类)

  1. String 字符串(系统类)
    学习系统类就是学习系统定义好的方法.
    java.lang 包 使用lang包 ,不用导入头文件(import 包名).
    字符串特点:字符串是常量 ,是不可变的.
    (一般字符串的方法都是有返回值的 例如拼接toString),字符串使用 和基本数据类型一样.
public class Kll {
    public static void main(String[] args) {
        /*String s1 = "abcd";
        s1 = "kll";
        System.out.println(s1);
        */
        //直接声明:abcd 是声明在常量池中
        String s2 = "abcd";
        //new 声明在堆内存中
        String s3 = new String("abcd");
        String s4 = "abcd";
        //== 比较的是对象的地址
        //qeuals 比较的是 字符场中每一个字符是否相等(比较的是字符串的值)
        System.out.println(s2 == s3);//false
        System.out.println(s2.equals(s3));//true
        System.out.println(s2 == s4);//true
        //s2和s3的区别
        /*
         s2表示的是一个对象 s3表示的是两个对象(new 和常量)
         */
    }
}

.获取
根据索引获取字符串中字符 :char(返回值类型) charAt(int index) 方法名
根据字符获取在字符串中的位置:int indexOf(int ch) ,int 类型是因为可以与char互转从开始找 0开始 第一个字符的索引
判断
是否包含这个字符串
boolean contains(CharSequence s)
是否以这个前缀开头
boolean startsWith(String prefix)
是否以这个后缀结尾
boolean endsWith(String suffix)
判断两个字符串相等
//boolean equals
判断两个字符串忽略大小写相等
boolean equalsIgnoreCase(anotherString)
字符串转小写
string toLowerCase(Locale locale)
字符串转大写
string toUpperCase(Locale locale)

//代码实例:
public class Kll {
    public static void main(String[] args) {
        //判断两个字符串相等
        //equals
        String s1 = "kll";
        boolean b1 = s1.equals("kll");
        System.out.println(b1);
       // 判断两个字符串忽略大小写相等
        boolean b2 = s1.equalsIgnoreCase("kll");
        System.out.println(b2);
       // 字符串转小写
       // toLowerCase(Locale locale)
        String s2 = "KLL";
        String s3 = s2.toLowerCase();
        System.out.println(s3);
       // 字符串转大写
      //  toUpperCase(Locale locale) 
        String s4 = s1.toUpperCase();
        System.out.println(s4);

    }

    private static void fun2() {
        //字符串包含判断
        String s1 ="konglinglei";
        boolean b1 =s1.contains("ling2");
        System.out.println(b1);
        //字符串拼接
        String s2 = "k";
        String s3 = "l";
        String s4 = s2 + s3;
        System.out.println(s4);
        String s5 = s2.concat(s3);
        System.out.println(s5);
        //是否以这个前缀开始
        String s6 = "www.baidu.com";
        boolean b2 = s6.startsWith("www");
        //是否以这个前缀结束
        boolean b3 = s6.endsWith(".com");
        System.out.println(b2 +" "+b3);
    }

    private static void fun1() {
        //根据索引返回对应的字符
        //相当于把字符串当做字符数组 下标从0开始
        //注意索引别越界
        String s1 = "konglinglei";
        char c1 = s1.charAt(4);
        System.out.println(c1);

        //根据字符获取在字符串中的位置(从开始找 0开始 第一个字符的索引)
        int index1 = s1.indexOf('i');
        System.out.println(index1);
        // 找第二个g  从传入的索引位置开始寻找
        int index2 = s1.indexOf('g', 3);
        System.out.println(index2);//打出来是3
        //参数  是字符串时
        int index3 = s1.indexOf("li");//返回的是第一个字符的位置
        System.out.println(index3);
        int index4 = s1.indexOf("li",4);//是从l找
        System.out.println(index4);
    }

string 中其他方法:
替换: string replace(char oldChar, char newChar) replace(String , String)
切割: string[] split(String regex) 按点切割 使用转义字符 \
获取子串: string substring(int beginIndex)
string substring(int beginIndex, int endIndex) 留头不留尾 [beginIndex, endIndex)
去空格: string trim()
比较: int compareTo(String anotherString)
把字符数组 转化为 字符串: String s1 = new String(字符数组名);
把字符串转化为 字符数组: char[] .toCharArray();
判断字符串是否为空: boolean .isEmpty()
基本数据类型 转化 为字符串: static string valueOf(数据类型 值)
快速遍历(只打印)
for(容器中元素类型 变量名: 遍历的容器) {
System.out.println(变量名);
}

代码实例:

public class Kll {
    public static void main(String[] args) {
        //fun1();
        //fun2();
        //fun3();
        //把字符数组 转化为 字符串
        char[] arr1 = {'a', 'b', 'c', 'd'};
        String s1 = new String(arr1);
        System.out.println(s1);
        //把字符串转化为 字符数组
        String s2 = "kll";
        char[] arr2 = s2.toCharArray();
        //快速遍历
        for(char c:arr2) {
            System.out.println(c);
        }
        //判断字符串是否为空
        //注意不要使用null 来调用方法 会出现空指针异常
        String s3 = "";
        boolean b1 = s3.isEmpty();
        System.out.println(b1);
        //基本数据类型 转化 为字符串 static string 
        //用类名调用
        String s4 = String.valueOf(15);
        System.out.println(s4);
    }

    private static void fun3() {
        //字符串比较 compareTo
        String s1 = "abC";
        String s2 = "acCaaaaa";
        //相等返回0
        //正值 前面大   反之
        //当字符串长度一样时 返回第一个不相等的两个字符的ASCII码差值
        //当字符串长度不等时 且未超出部分相等时 返回的是两个字符串长度的差值
                        //未超出部分不等时 判断方式和长度一样时的方式相同
        int result = s1.compareTo(s2);
        System.out.println(result);
    }

    private static void fun2() {
        //获取子串 string  substring(int beginIndex) 
        //获取子串 string  substring(int beginIndex, int endIndex) 
        String s1 = "konglinglei";
        String s2 = s1.substring(3);
        System.out.println(s2);
        System.out.println(11);
        //留头不留尾
        String s3 = s1.substring(4, 11);
        System.out.println(s3);
        //去空格
        String s4 = "   kong   ling  lei     ";
        String s5 = s4.trim();
        System.out.println(s5);
    }

    private static void fun1() {
        //替换 string replace(char oldChar, char newChar) 
        String s1 = "konglinglei";
        String s2 = s1.replace("g", "Aa");
        System.out.println(s2);
        //切割 split(String regex) 
        String[] str1 = s1.split("g");
        System.out.println(Arrays.toString(str1));
        //快速遍历(只打印)
        for(String s: str1) {
            System.out.println(s);
        }
        String s3 = "www.baidu.com";
        //按点切割 使用转义字符 \\
        String[] str2 = s3.split("\\.");
        System.out.println(Arrays.toString(str2));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值