常用API(三)之String

包名 java.lang.String
Java 程序中的所有字符串字面值(如 “abc” )都作为此类的实例实现。即,程序中双引号,都是string类的对象。
特点:
1.字符串的内容创建后不能更改
2.正是字符串不可改变,所以字符串可以共享使用。
3.字符串效果上相当于char[]字符数组,但底层原理是byte[]字节数组。

创建字符串的常见3+1种方式
三种构造方法,一种直接创建

public String();创建一个空白字符串
public String(char[] array);根据字符数组的内容,来创建对应的字符串
public String(byte[] array);根据字节数组的内容,来创建对应的字符串
String str4 ="Hello,World!";
public class Demo01String {
    public static void main(String[] args) {
        //空参构造
        String str1 = new String();
        //根据字符数组创建字符串
        char[] charArray = {'a','b','c','d'};
        String str2 = new String(charArray);
        System.out.println(str2);
        //根据字节数组创建字符串
        byte[] byteArray = {97,98,99};
        String str3 =new String(byteArray);
        System.out.println(str3);
        //直接创建
        String str4 ="Hello,World!";
        System.out.println(str4);
    }
}

字符串常量池:程序中直接写上双引号的字符串,就在字符串常量池中。

对于基本数据类型,==比较的是数值

对于引用类型来说,==比较的是【地址值】

public class Demo02StringPool {
    public static void main(String[] args) {
        String str1 = "abc";
        String str2 = "abc";
        char[] chars = {'a', 'b', 'c'};
        String str3 = new String(chars);

        System.out.println(str1 == str2);//true
        System.out.println(str1 == str3);//false
    
    }
}

在这里插入图片描述比较字符串内容

public boolean equals(Object obj);参数可以使任何对象,只有参数是一个字符串,且内容相同才为true。

备注:
1.任何对象都能用Object进行接收。
2.equals()方法具有对称性 a.equals(b)和b.equals(a)是一样的。
3.如果一个常量一个变量,推荐常量字符串放前面。
推荐"abc".equals(b);不推荐b.equals(“abc”)

public boolean equalsIgnoreCase(Strnig str),忽略大小写的比较


public class Demo03Methon {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        char[] chars = {'H', 'e', 'l', 'l', 'o'};
        String str3 = new String(chars);
        System.out.println(str1.equals(str2));//true
        System.out.println(str2.equals(str3));//true
        System.out.println(str3.equals("Hello"));//true
        System.out.println("Hello".equals(str1));//true
        
       /*如果一个常量一个变量,推荐常量字符串放前面。
        推荐"abc".equals(b);不推荐b.equals("abc")*/
       String str4 = null;
        System.out.println("abc".equals(str4));//推荐False
        //System.out.println(str4.equals("abc"));//不推荐;报错, 空指针异常

        String strA="Moon";
        String strB="moon";
        System.out.println(strA.equalsIgnoreCase(strB));//True
    }
}

字符串获取的方法
public int leng();获取字符串长度
public String concat(String str)将当前字符串和参数字符串拼接,返回新的字符串
public char charAt(int index)获取索引位置的单个字符
public int indexOf(String str)查找字符/字符串在原字符串中首次出现的位置,若没有就返回-1。

public class Demo04StringGet {
    public static void main(String[] args) {
        int a = "asdasdasdasdasdasd".length();
        System.out.println(a);

        String str1 = "Hello";
        String str2 = "World";
        String str3 = str1.concat(str2);
        System.out.println(str1);
        System.out.println(str2);//原封不动World
        System.out.println(str3);//new!

        char b = str1.charAt(3);
        System.out.println(b);

        int c =str2.indexOf("ld");
        System.out.println(c);
        System.out.println(str2.indexOf("44"));//-1
    }
}

字符串的截取方法
public String substring(int index)截取参数位置一直到字符串末尾,返回新的字符串
public String substring(int begin,int end)截取从begin开始,end为止中间的字符,
Tip:[begin,end)左闭右开区间

public class Demo05SubString {
    public static void main(String[] args) {
        String str1 = "HelloWorld";
        String str2 = str1.substring(5);
        System.out.println(str2);//new!
        System.out.println(str1);//没变

        String str3 = str1.substring(3, 6);
        System.out.println(str3);
    }
}

String当中转换相关的常用方法
public char[] toCharArray();将当前字符串拆分成为字符数组作为返回值
public byte[] getBytes();获取当前字符串底层字节数组
public String replace(CharSquence oldString,CharSquence newString)将所有出现的老字符串返回替换,之后的结果为新字符串
CharSquence是可以接收字符串类型的

public class Demo06StringConvert {
    public static void main(String[] args) {
        char[] chars = "hello".toCharArray();
        System.out.println(chars[0]);
        System.out.println(chars[1]);
        System.out.println(chars[2]);

        byte[] bytes = "abc".getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }

        String str1 = "How are you doing today?";
        String str2 = str1.replace("o","*");
        System.out.println(str1);//不变
        System.out.println(str2);//新的字符串
    }
}

字符串的分割方法
public String[] split(String regex);
按照参数规则,将字符串切分为若干部分
Tip:split 方法的参数其实是一个正则表达式,
如果按照英文句点".“进行切分,必须写”\."才行

public class Demo07StringSplit {
    public static void main(String[] args) {
        String str1 = "aaa bbb ccc";
        String[] split1 = str1.split(",");
        for (int i = 0; i < split1.length; i++) {
            System.out.println(split1[i]);
        }
        //split 方法的参数其实是一个正则表达式,
        //如果按照英文句点"."进行切分,必须写"\\."
        String str2 = "aaa.bbb.ccc";
        String[] split2 = str1.split(".");
        System.out.println(split2.length);//0
        for (int i = 0; i < split2.length; i++) {
            System.out.println(split2[i]);
        }
    }
}

练习1
将整形数组{1,2,3}转化为String类型特定格式[Word1#Word2#Word3]。

public class Test01 {
    public static void main(String[] args) {
        int[] array01 = {1, 2, 3};
        String str = arrayToString(array01);
        System.out.println(str);
    }

    public static String arrayToString(int[] array) {
        String str = "[";
        String temp = new String();
        for (int i = 0; i < array.length; i++) {
            if (i == array.length - 1) {
                str += "Word" + array[i] + "]";
            } else {
                str += "Word" + array[i]+"#";
            }
        }
        return str;
    }
}

练习2
字符串种类的统计
键盘输入一个字符串统计其中的 大写字母, 小写字母,数字,其他的个数。

import java.util.Scanner;
public class Test02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int big = 0, min = 0, num = 0, other = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) >= 48 && str.charAt(i) <= 57) {
                num++;
            } else if (str.charAt(i) >= 65 && str.charAt(i) <= 90) {
                big++;
            } else if (str.charAt(i) >= 97 && str.charAt(i) <= 122) {
                min++;
            } else {
                other++;
            }
        }
        System.out.println("数字有:" + num);
        System.out.println("大写字母有:" + big);
        System.out.println("小写字母有:" + min);
        System.out.println("其他字符有:" + other);
    }
}

Tip:String.cahrAt(i)不能与“A”类似的字符型进行比较。

str.charAt(i) >= "a" //error
Error:(13, 31) java: 二元运算符 '>=' 的操作数类型错误
  第一个类型:  char
  第二个类型: java.lang.String

而char类型可以与“A”这样的字符比较,进行字符类型的判断。
或者
将字符串转化为数组,使用toCharArray()方法,再进行遍历判断。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值