String类

作者: 丫甘九 | 来源:发表于2018-11-25 17:32 被阅读0次

字符串概述

  • 字符串:就是由多个字符组成的一串数据,也可以看成是一个字符数组
  • 通过查看API,我们也可以知道
    A:字符串字面值“abc”也可以看成是一个字符串数组
    B:字符串是常量,一旦被赋值,就不可以被更改
构造方法
  • public String();
    (空构造)
  • public String(byte[] bytes);
    (把字节数组转成字符串)
  • public String(byte[] bytes,int index(索引),int length);
    (把字节数组的一部分转成字符串)
  • public String(char[] value);
    (把字符数组转成字符串)
  • public String(char[] value,int index(索引),int count);
    (把字符数组的一部分转成字符串)
  • public String(String original);
    (把字符串常量转成字符串)
字符串方法
  • public int length();返回此字符串的长度
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字面值对象和构造方法创建对象的区别

  • String s = new String("hello");String s = "hello";
    这俩个有区别么?
    有,前者会创建俩个对象,后者只创建一个对象
  • ==:比较引用类型,比较的是地址值是否相同
  • equals:比较引用类型,默认也是比较地址值是否相同,而String()重写了equals()方法,比较的是内容是否相同
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类的判断功能

  • boolean equals(Object obj);
    (比较字符串内容是否相同)
  • boolean equalsIgnoreCase(String str);
    (比较字符串的内容是否相同,忽略大小写)
  • boolean contains(String str);
    (判断大字符串中是否包含小字符串)
  • boolean startsWith(String str);
    (判断字符串是否以某个指定的字符串开头)
  • boolean endWith(String str);
    (判断字符串是否以某个指定的字符串结尾)
  • boolean isEmpty();
    (判断字符串是否为空)
    注:字符串内容为空和字符串对象为空不一样
    例:String s = " ";//表示有内容,但是内容是空的,可以调方法
    String s = null;//表示没有内容,不能调方法
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类的获取功能

  • int length();
    (获取字符串的长度)
  • char charAt(int index);
    (获取指定索引位置的字符)
  • int indexOf(int ch);
    (返回指定字符在此字符串中第一次出现处的索引)
    这里为什么是int类型而不是char类型呢?
    原因是‘a’和97其实都可以代表a
  • int indexOf(String str);
    (返回指定字符串在此字符串中第一次出现处的索引)
  • int indexOf(int ch,int fromIndex);
    (返回指定字符在此字符串中从指定位置后第一次出现处的索引)
  • int indexOf(String str,int fromIndex);
    (返回指定字符串在此字符串中从指定位置后第一次出现处的索引)
  • String substring(int start);
    (从指定位置开始截取字符串,默认到末尾,包含start这个索引)
  • String substring(int start,int end);
    (从指定位置开始到指定位置结束截取字符串,包括start索引,但是不包括ebd索引)
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类的转换功能

  • byte[] getbytes();
    (把字符串转换为字节数组)
  • char[] tocharArray();
    (把字符串转换为字符数组)
  • static String valueOf(char[] chs);
    (把字符数组转换成字符串)
  • static String valueOf(int i);
    (把int类型的数据转换成字符串)
    注:String类型的valueOf()方法可以把任何类型的数据转换成字符串
  • String toLowerCase();
    (把字符串转成小写)
  • String toUpperCase();
    (把字符串转成大写)
  • String concat(String str);
    (把字符串拼接)
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

字符串的其他功能

  • 替换功能
    String replace(char old,char new);
    String replace(String old,String new);
  • 去除字符串俩空格
    String trim();
  • 按字典顺序比较俩字符串
    int compareTo(String str)
    int compareToIgnoreCase(String str)
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
//哈希码值相减

相关文章

  • String

    1.String类2.String类和常量池3.String类常用的方法 1.String类 1.String类又...

  • String

    String UML类图 String 被 final 修饰,String 类不可被继承,不可变类。 String...

  • Java之String类

    String 1. String类基本特性 String类再Java类中也是一个很重要的类,String类的对象是...

  • string类

    string类 1. string类常用方法 2. string类常用方法2 3. string类的查找 判断字符...

  • 二十五、String与StringBuffer/StringBu

    一、 String类 1. String类概述 查阅API中的String类的描述,发现String 类代表字符串...

  • String类概述及其构造方法

    构造方法 String类的判断功能 String类的获取功能 String的转换功能 String类的其他功能

  • Java String总结

    Java String总结 String 类 String类定代码如下: 从代码看有两点需要注意: String类...

  • 8.2-全栈Java笔记:字符串相关类(String/Strin

    字符串相关类(String/StringBuilder /StringBuffer) String类 String...

  • 10.常用API—String

    常用API(String,ArrayList) String String类概述 String类定义的变量可以用于...

  • 第四章 复合类型(3)string类简介

    (三)string类简介 1.string类的优势: 使用string类的时候,必须在头文件中包含string,...

网友评论

      本文标题:String类

      本文链接:https://www.haomeiwen.com/subject/dobpqqtx.html