栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java基础之String类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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("******************");
        
        byte[] b={98,97,99,100,101};
        String s1= new String(b);
        System.out.println(s1);
        System.out.println(s1.length());
        System.out.println("*******************");
        
        String s2 = new String(b, 0, 3);
        System.out.println(s2);
        System.out.println(s2.length());
        System.out.println("********************");
        
        char[] c={'a','b','c'};
        String s3 = new String(c);
        System.out.println(s3);
        System.out.println(s3.length());
        System.out.println("*********************");
        
        String s4 = new String(c, 0, 2);
        System.out.println(s4);
        System.out.println(s4.length());
        System.out.println("**********************");
        
        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) {
        
        String s1="abcdefg";
        String s2="Abcdefg";
        System.out.println(s1.equals(s2));
        System.out.println("===============");
        
        System.out.println(s1.equalsIgnoreCase(s2));
        System.out.println("===============");
        
        System.out.println(s1.contains("ab"));
        System.out.println("===============");
        
        System.out.println(s1.startsWith("a"));
        System.out.println("===============");
        
        System.out.println(s1.endsWith("e"));
        System.out.println("===============");
        
        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) {
        
        String s="helloworld";
        System.out.println(s.length());
        System.out.println("=======================");
        
        System.out.println(s.charAt(0));
        System.out.println("=======================");
        
        System.out.println(s.indexOf('o'));
        System.out.println("=======================");
        
        System.out.println(s.indexOf("he"));
        System.out.println("=======================");
        
        System.out.println(s.indexOf('l',2));
        System.out.println("=======================");
        
        System.out.println(s.indexOf("ll",2));
        System.out.println("=======================");
        
        System.out.println(s.substring(1));
        System.out.println("=======================");
        
        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";
        
        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[] 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'};
        
        String s = String.valueOf(c);
        System.out.println(s);
        System.out.println("===============================");
        
        String s1 = String.valueOf(100);
        System.out.println(s1);
        System.out.println("===============================");
        
        String s2 = s.toLowerCase();
        System.out.println(s2);
        System.out.println("===============================");
        
        String s3 = s.toUpperCase();
        System.out.println(s3);
        System.out.println("===============================");
        
        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 = s.replace('l', 'a');
        System.out.println(replace);
        System.out.println("================================");
        
        String replace1 = s.replace("ll", "aa");
        System.out.println(replace1);
        System.out.println("================================");
        
        String s1=" hello  world  ";
        String trim = s1.trim();
        System.out.println(s1);
        System.out.println(trim);
        System.out.println("================================");
        
        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;//连对象都没有调用方法时会出现空指针异常

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/332514.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号