String类

2018-11-25  本文已影响0人  丫甘九

字符串概述

构造方法
字符串方法
public class Stringdemo {
public static void main(String[] args) {
    //public String();
    //(空构造)
    String s1 = new String();
    System.out.println("s1:"+s1);
    System.out.println("s1.length:"+s1.length());
    System.out.println("------------------------");
    //public String(byte[] bytes);
    //(把字节数组转成字符串)
    byte[] bys = {97,98,99,100,101};
    //会把字节转换成对应的哈希码值,从而转换成字符
    String s2 = new String(bys);
    System.out.println("s2:"+s2);
    System.out.println("s2.length:"+s2.length());
    System.out.println("------------------------");
    //public String(byte[] bytes,int index(索引),int length);
    //(把字节数组的一部分转成字符串)
    String s3 = new String(bys,1,3);
    //表示的是从第一个字节的位置开始,到第三个字节结束,转换成对应的字符串
    System.out.println("s3:"+s3);
    System.out.println("s3.length:"+s3.length());
    System.out.println("------------------------");
    //public String(char[] value);
    //(把字符数组转成字符串)
    char[] chs = {'a','b','c','d','e','张','国','荣'};
    String s4 = new String(chs);
    System.out.println("s4:"+s4);
    System.out.println("s4.length:"+s4.length());
    System.out.println("------------------------");
    //public String(char[] value,int index(索引),int count);
    //(把字符数组的一部分转成字符串)
    String s5= new String(chs,2,5);
    System.out.println("s5:"+s5);
    System.out.println("s5.length:"+s5.length());
    System.out.println("------------------------");
    //public String(String original);
    //(把字符串常量转成字符串)
    String s6 = "abcde";
    System.out.println("s6:"+s6);
    System.out.println("s6.length:"+s6.length());
    
}
}

结果为:
s1:
s1.length:0
------------------------
s2:abcde
s2.length:5
------------------------
s3:bcd
s3.length:3
------------------------
s4:abcde张国荣
s4.length:8
------------------------
s5:cde张国
s5.length:5
------------------------
s6:abcde
s6.length:5

String类的特点

String s = "hello";
s+="world";
system.out.println("s:"+s);

结果为:
helloworld
字符串特点:

A:字符串直接赋值的方法是先到字符串常量池里面去找,如果有就返回,如果没有就创建并返回
B:一旦被赋值就不能被改变(说的是值不能变,不是字符串不能变)

CC2@0IGE~@{(L5WPR$IELIK.png

String字面值对象和构造方法创建对象的区别

public class Stringdemo2 {
public static void main(String[] args) {
    String s1 = new String("hello");
    String s2 = "hello";
    System.out.println(s1==s2);
    System.out.println(s1.equals(s2));
}
}


结果为:
false
true

![J14EF]8P)(FO@KS6%C4NRFX.png](https://img.haomeiwen.com/i14016997/96df199e5a5f3eed.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

String类的判断功能

public class Stringdemo3 {
    public static void main(String[] args) {
        //创建字符串对象
        String s1 = "helloworld";
        String s2 = "helloworld";
        String s3 = "Helloworld";
        //boolean equals(Object obj);
        //(比较字符串内容是否相同,区分大小写)
        System.out.println("equals:"+s1.equals(s2));
        System.out.println("equals:"+s1.equals(s3));
        System.out.println("--------------------------");
        //boolean equalsIgnoreCase(String str);
        //(比较字符串的内容是否相同,忽略大小写)
        System.out.println("equalsIgnoreCase:"+s1.equalsIgnoreCase(s2));
        System.out.println("equalsIgnoreCase:"+s1.equalsIgnoreCase(s3));
        System.out.println("--------------------------");
        //boolean contains(String str);
        //(判断大字符串中是否包含小字符串)
        System.out.println("contains:"+s1.contains("hello"));
        System.out.println("contains:"+s1.contains("hw"));
        System.out.println("--------------------------");
        //boolean startsWith(String str);
        //(判断字符串是否以某个指定的字符串开头)
        System.out.println("startsWith:"+s1.startsWith("hello"));
        System.out.println("startsWith:"+s1.startsWith("hw"));
        System.out.println("--------------------------");
        //boolean endWith(String str);
        //(判断字符串是否以某个指定的字符串结尾)
        System.out.println("endWith:"+s1.endsWith("hello"));
        System.out.println("endWith:"+s1.endsWith("world"));
        System.out.println("--------------------------");
        //boolean isEmpty();
        //(判断字符串是否为空)
        System.out.println("endWith:"+s1.isEmpty());
        System.out.println("endWith:"+s1.isEmpty());
        String s4 = "";
        String s5 = null;
        System.out.println("endWith:"+s4.isEmpty());
        System.out.println("endWith:"+s5.isEmpty());//error,因为连对象都没有
    }

}

结果为:
equals:true
equals:false
--------------------------
equalsIgnoreCase:true
equalsIgnoreCase:true
--------------------------
contains:true
contains:false
--------------------------
startsWith:true
startsWith:false
--------------------------
endWith:false
endWith:true
--------------------------
endWith:false
endWith:false
endWith:true
Exception in thread "main" java.lang.NullPointerException
    at Stringdemo3.main(Stringdemo3.java:40)

String类的获取功能

public class Stringdemo4 {
    public static void main(String[] args) {
        //定义一个字符串对象
        String s = "helloworld";
        //int length();
        //(获取字符串的长度)
        System.out.println("s.length:"+s.length());
        System.out.println("------------------------");
        //char charAt(int index);
        //(获取指定索引位置的字符)
        System.out.println("s.charAt:"+s.charAt(7));
        System.out.println("------------------------");
        //int indexOf(int ch);
        //(返回指定字符在此字符串中第一次出现处的索引)
        System.out.println("s.indexOf:"+s.indexOf("l"));
        System.out.println("------------------------");
        //int indexOf(String str);
        //(返回指定字符串在此字符串中第一次出现处的索引)
        System.out.println("s.indexOf:"+s.indexOf("low"));
        System.out.println("------------------------");
        //这个位置说的是字符串首字母出现的位置
        //int indexOf(int ch,int fromIndex);
        //(返回指定字符在此字符串中从指定位置后第一次出现处的索引)
        System.out.println("s.indexOf:"+s.indexOf("l",4));
        System.out.println("------------------------");
        //int indexOf(String str,int fromIndex);
        //(返回指定字符串在此字符串中从指定位置后第一次出现处的索引)
        System.out.println("s.indexOf:"+s.indexOf("llo",4));
        System.out.println("------------------------");
        //String substring(int start);
        //(从指定位置开始截取字符串,默认到末尾,包含start这个索引)
        System.out.println("s.indexOf:"+s.substring(1));
        System.out.println("------------------------");
        //String substring(int start,int end);
        //(从指定位置开始到指定位置结束截取字符串,包括start索引,但是不包括ebd索引)
        System.out.println("s.indexOf:"+s.substring(1,4));
        System.out.println("------------------------");
        
    }

}

结果为:
s.length:10
------------------------
s.charAt:r
------------------------
s.indexOf:2
------------------------
s.indexOf:3
------------------------
s.indexOf:8
------------------------
s.indexOf:-1
------------------------
s.indexOf:elloworld
------------------------
s.indexOf:ell
------------------------

字符串的遍历

需求:

遍历获取字符串中的每一个字符

分析:

A:如何能拿到每一个字符呢?
char charAt(int index);
B:我怎么知道字符到底有多少个呢?
int length();

public class Stringdemo5 {
public static void main(String[] args) {
    String s = "helloworld";
    //原始版本
    /*System.out.println(s.charAt(0));
    System.out.println(s.charAt(1));
    System.out.println(s.charAt(2));
    System.out.println(s.charAt(3));
    System.out.println(s.charAt(4));
    System.out.println(s.charAt(5));
    System.out.println(s.charAt(6));
    System.out.println(s.charAt(7));
    System.out.println(s.charAt(8));
    System.out.println(s.charAt(9));*/
    //我们只需要从零取到九
    /*for(int x=0;x<=9;x++) {
        System.out.println(s.charAt(x));
    }*/
    //如果长度特别长,不可能去数,所以我们就用到了长度方法
    for(int x=0;x<s.length();x++) {
        System.out.println(s.charAt(x));
    }
}
}

结果为:
h
e
l
l
o
w
o
r
l
d

String类的转换功能

public class Stringdemo6 {
    public static void main(String[] args) {
        //定义一个字符串对象
        String s = "JavaSE";
        //byte[] getbytes();
        //(把字符串转换为字节数组)
        byte[] bys = s.getBytes();
        for(int x = 0;x<bys.length;x++) {
            System.out.println(bys[x]);
        }
        System.out.println("--------------------");
        //char[] tocharArray();
        //(把字符串转换为字符数组)
        char[] chs = s.toCharArray();
        for(int x = 0;x<chs.length;x++) {
            System.out.println(chs[x]);
        }
        System.out.println("--------------------");
        //static String valueOf(char[] chs);
        //(把字符数组转换成字符串)
        String ss  =  String.valueOf(chs);
        System.out.println(ss);
        System.out.println("--------------------");
        //static String valueOf(int i);
        //(把int类型的数据转换成字符串)
        int i = 100;
        String sss = String.valueOf(i);
        System.out.println(sss);
        System.out.println("--------------------");
        //String toLowerCase();
        //(把字符串转成小写)
        System.out.println("toLowerCase:"+s.toLowerCase());
        System.out.println("s:"+s);
        System.out.println("--------------------");
        //String toUpperCase();
        //(把字符串转成大写)
        System.out.println("toUpperCase:"+s.toUpperCase());
        System.out.println("s:"+s);
        System.out.println("--------------------");
        //String concat(String str);
        //(把字符串拼接)
        String s1 = "hello";
        String s2 = "world";
        String s3 = s1+s2;
        System.out.println(s3);
    }

}

结果为:
74
97
118
97
83
69
--------------------
J
a
v
a
S
E
--------------------
JavaSE
--------------------
100
--------------------
toLowerCase:javase
s:JavaSE
--------------------
toUpperCase:JAVASE
s:JavaSE
--------------------
helloworld

字符串的其他功能

public class Stringdemo7 {
    public static void main(String[] args) {
        //替换功能
        String s1  =  "helloworld";
        String s2  =  s1.replace("l", "k");
        String s3  = s1.replace("owo","ak47");
        System.out.println("s1:"+s1);
        System.out.println("s1:"+s2);
        System.out.println("s1:"+s3);
        System.out.println("-----------------");
        //去除字符串俩空格
        String s4 = "helloworld";
        String s5 = s4.trim();
        System.out.println("s4:"+s4 +"-------------");
        System.out.println("s5:"+s5 +"-------------");
        System.out.println("-----------------");
        //按字典顺序比较俩字符串
        String s6 = "hello";
        String s7 = "hello";
        String s8 = "abc";
        String s9 = "syz";
        System.out.println(s6.compareTo(s7));
        System.out.println(s6.compareTo(s8));
        System.out.println(s6.compareTo(s9));
        
    }

}

结果为:
s1:helloworld
s1:hekkoworkd
s1:hellak47rld
-----------------
s4:helloworld-------------
s5:helloworld-------------
-----------------
0
7
-11
//哈希码值相减
上一篇 下一篇

猜你喜欢

热点阅读