java基础之String类

目录

1:String类概述

2:构造方法

3: String类的判断功能

4:String类的获取功能

5:String类的转换功能

6:String类的其他功能

7:注意


1:String类概述

字符串是由多个字符组成的一串数据(字符序列) 字符串可以看成是字符数组

java.lang包下的,不用导包。

2:构造方法

(1)public String()

(2)public String(byte[] bytes) 将一个字节数组转成一个字符串

(3)public String(byte[] bytes,int offset,int length)将字节数组中的一部分截取出来变成一个字符串 offset指的是偏移量或者索引

(4)public String(char[] value)将一个字符数组转化成一个字符串

(5)public String(char[] value,int offset,int count)将字符数组中的一部分截取出来变成一个字符串

(6)public String(String original)

public class Text11 {
    public static void main(String[] args) {
        //public String()
        String s = new String();
        System.out.println(s);
        System.out.println(s.length());
        System.out.println("******************");
        /*public String(byte[] bytes)
        将一个字节数组转成一个字符串*/
        byte[] b={98,97,99,100,101};
        String s1= new String(b);
        System.out.println(s1);
        System.out.println(s1.length());
        System.out.println("*******************");
        /*public String(byte[] bytes,int offset,int length)将字节数组中的一部分截取出来
                变成一个字符串 offset指的是偏移量或者索引*/
        String s2 = new String(b, 0, 3);
        System.out.println(s2);
        System.out.println(s2.length());
        System.out.println("********************");
        /*public String(char[] value)将一个字符数组转化成一个字符串*/
        char[] c={'a','b','c'};
        String s3 = new String(c);
        System.out.println(s3);
        System.out.println(s3.length());
        System.out.println("*********************");
        /*public String(char[] value,int offset,int count)
        将字符数组中的一部分截取出来变成一个字符串*/
        String s4 = new String(c, 0, 2);
        System.out.println(s4);
        System.out.println(s4.length());
        System.out.println("**********************");
        /*public String(String original)*/
        String s5 = new String("student");
        System.out.println(s5);
        System.out.println(s5.length());


    }
}

3: String类的判断功能

(1)boolean equals(Object obj) 比较字符串中的内容是否相同 区分大小写

(2)boolean equalsIgnoreCase(String str) 比较字符串中的内容是否相同 忽略大小写

(3)boolean contains(String str)

//判断大的字符串中是否包含传入的字符串,如果包含,返回的是true,否则就是false

//区分大小写

(4)boolean startsWith(String str)测试此字符串是否以指定的前缀开头。

//区分大小写

(5)boolean endsWith(String str)

//区分大小写

(6)boolean isEmpty()判断是否为空

public class Text12 {
    public static void main(String[] args) {
        /*boolean equals(Object obj)
        比较字符串中的内容是否相同 区分大小写
         */
        String s1="abcdefg";
        String s2="Abcdefg";
        System.out.println(s1.equals(s2));
        System.out.println("===============");
        /*boolean equalsIgnoreCase(String str)
        比较字符串中的内容是否相同 忽略大小写*/
        System.out.println(s1.equalsIgnoreCase(s2));
        System.out.println("===============");
        /*boolean contains(String str)
        判断大的字符串中是否包含传入的字符串,如果包含,返回的是true,否则就是false
           区分大小写*/
        System.out.println(s1.contains("ab"));
        System.out.println("===============");
        /*boolean startsWith(String str)测试此字符串是否以指定的前缀开头。
           区分大小写*/
        System.out.println(s1.startsWith("a"));
        System.out.println("===============");
        /*(5)boolean endsWith(String str)
        判断字符串是否以指定的字符结尾区分大小写*/
        System.out.println(s1.endsWith("e"));
        System.out.println("===============");
        /*boolean isEmpty()判断是否为空*/
        System.out.println(s1.isEmpty());





    }
}

 

4:String类的获取功能

(1):int length() 获取字符串的长度

(2):public char charAt(int index)返回char指定索引处的值

(3):public int indexOf(int ch)返回指定字符第一次出现的字符串内的索引

(4):public int indexOf(String str)

// 返回指定子字符串第一次出现的字符串内的索引

当子字符串不存在的时候,返回的是-1

(5):public int indexOf(int ch,int fromIndex)

// 返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索

(6):int indexOf(String str,int fromIndex)

//返回指定字符串第一次出现的字符串内的索引,以指定的索引开始搜索

(7):public String substring(int beginIndex)

// 返回一个字符串,该字符串是此字符串的子字符串。

// 子字符串以指定索引处的字符开头,并扩展到该字符串的末尾

(8):public String substring(int beginIndex,int endIndex)

// 返回一个字符串,该字符串是此字符串的子字符串。

// 子串开始于指定beginIndex并延伸到字符索引endIndex - 1 。

//左闭右开

public class Text15 {
    public static void main(String[] args) {
        /*int length() 获取字符串的长度*/
        String s="helloworld";
        System.out.println(s.length());
        System.out.println("=======================");
        /*public char charAt(int index)返回char指定索引处的值*/
        System.out.println(s.charAt(0));
        System.out.println("=======================");
        /*public int indexOf(int ch)返回指定字符第一次出现的字符串内的索引*/
        System.out.println(s.indexOf('o'));
        System.out.println("=======================");
        /*public int indexOf(String str)
         返回指定子字符串第一次出现的字符串内的索引
         当子字符串不存在的时候,返回的是-1*/
        System.out.println(s.indexOf("he"));
        System.out.println("=======================");
        /*public int indexOf(int ch,int fromIndex)
           返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索*/
        System.out.println(s.indexOf('l',2));
        System.out.println("=======================");
        /*int indexOf(String str,int fromIndex)
        返回指定字符串第一次出现的字符串内的索引,以指定的索引开始搜索*/
        System.out.println(s.indexOf("ll",2));
        System.out.println("=======================");
        /*public String substring(int beginIndex)
          返回一个字符串,该字符串是此字符串的子字符串。
          子字符串以指定索引处的字符开头,并扩展到该字符串的末尾*/
        System.out.println(s.substring(1));
        System.out.println("=======================");
        /*public String substring(int beginIndex,int endIndex)
            返回一个字符串,该字符串是此字符串的子字符串。
             子串开始于指定beginIndex并延伸到字符索引endIndex - 1 。左闭右开*/
        System.out.println(s.substring(1,3));


    }
}

 

5:String类的转换功能

(1)public byte[] getBytes()

// 使用平台的默认字符集将此String编码为字节序列,将结果存储到新的字节数组中

(2)char[] toCharArray()将此字符串转换为新的字符数组。// 字符串 ---> 字符数组

public class Text16 {
    public static void main(String[] args) {
        String s="helloword";
        /*public byte[] getBytes()
         使用平台的默认字符集将此String编码为字节序列,将结果存储到新的字节数组中*/
        System.out.print("[");
        byte[] bytes = s.getBytes();
        for (byte aByte : bytes) {
            if(aByte==bytes[bytes.length-1])
            {System.out.println(aByte+","+"]");}
            else{
                System.out.print(aByte+",");
            }
        }
        /*char[] toCharArray()将此字符串转换为新的字符数组*/
        char[] chars = s.toCharArray();
        System.out.print("[");
        for (char aChar : chars) {
            if(aChar==bytes[bytes.length-1])
            {System.out.print(aChar+","+"]");}
            else{
                System.out.print(aChar+",");
            }
        }


    }
}

 

(3)字符数组 ----> 字符串

static String valueOf(char[] chs)

(4) static String valueOf(int i)

//将int类型的数据转化成字符串类型

(5)String toLowerCase()

//将字符串内容全部转化成小写

(6)String toUpperCase()

//将字符串内容全部转大写

(7)public String concat(String str)将指定的字符串连接到该字符串的末尾。

//将小括号里面的字符串拼接到调用方法的字符串后面

public class Text17 {
    public static void main(String[] args) {
        char[] c={'a','A','c','d'};
        /*字符数组 ----> 字符串static String valueOf(char[] chs)*/
        String s = String.valueOf(c);
        System.out.println(s);
        System.out.println("===============================");
        /*static String valueOf(int i)
           将int类型的数据转化成字符串类型*/
        String s1 = String.valueOf(100);
        System.out.println(s1);
        System.out.println("===============================");
        /*String toLowerCase()将字符串内容全部转化成小写*/
        String s2 = s.toLowerCase();
        System.out.println(s2);
        System.out.println("===============================");
        /*String toUpperCase()
            将字符串内容全部转大写*/
        String s3 = s.toUpperCase();
        System.out.println(s3);
        System.out.println("===============================");
        /*public String concat(String str)将指定的字符串连接到该字符串的末尾。
        将小括号里面的字符串拼接到调用方法的字符串后面*/
        String s4 = s.concat("efh");
        System.out.println(s4);


    }
}

 

6:String类的其他功能

(1)String replace(char old,char new)

//将字符串中所有的l替换成a,返回一个新的字符串

(2)String replace(String old,String new)

将指定字符串替换如果被替换的字符串不存在,返回原来的字符串

(3)String trim()去除字符串两边的空格,不能去除字符串中间的空格

(4)int compareTo(String str)//比较两个字符串如果相等就返回0,,如果不相等依次比较ASCII码值如果个数相同就依次比较,如果不相同就输出个数的差值

public class Text18 {
    public static void main(String[] args) {
        String s="helloworld";
        /*String replace(char old,char new)
          将字符串中所有的l替换成a,返回一个新的字符串*/
        String replace = s.replace('l', 'a');
        System.out.println(replace);
        System.out.println("================================");
        /*String replace(String old,String new)
     将指定字符串替换如果被替换的字符串不存在,返回原来的字符串*/
        String replace1 = s.replace("ll", "aa");
        System.out.println(replace1);
        System.out.println("================================");
        /*String trim()去除字符串两边的空格,不能去除字符串中间的空格*/
        String s1=" hello  world  ";
        String trim = s1.trim();
        System.out.println(s1);
        System.out.println(trim);
        System.out.println("================================");
        /*int compareTo(String str)//比较两个字符串如果相等就返回0
           如果不相等依次比较ASCII码值如果个数相同就依次比较,如果不相同就输出个数的差值*/
        System.out.println(s.compareTo(s1));

    }
}

7:注意

1、==比较引用数据类型比较的是地址值

2、equals默认比较的是地址值,但是由于String类中重写了该方法,所以比较的是内容

3、String s = new String(“hello”) 会在堆内存中创建对象

4、字符串如果是变量相加,是先开辟空间,然后再拼接

5、字符串如果是常量相加,是先加,然后再去常量池里找,如果找到了就返回,如果找不到就创建

6、System.identityHashCode(s3) 获取字符串的地址值

7、字符串的特点:一旦被赋值,就不能被改变

8、在进行字符串内容比较的时候,为了防止出现空指针异常,将变量放后面

9、String s7 = null;//连对象都没有调用方法时会出现空指针异常

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值