字符串及其API

字符串

字符串特点

  1. 字符串的内容永不可改变
  2. 字符串可以共享使用;
  3. 字符串效果上相当于char[]字符数组,但底层是byte[]字节数组;

基本用法

1.创建

常见创建字符串的4种方式
  1. 使用空参数创建一个String

    String str1 = new String();

  2. 第二种:根据字符数组创建一个String

    ​ char[] charArray = {‘G’, ‘o’, ‘o’, ‘d’};
    ​ String str2 = new String(charArray);

  3. 第三种:根据字节数组创建一个String

    ​ byte[] byteArray = {97,98,99};
    ​ String str3 = new String(byteArray);

    byte数组里的数字会根据ASCii表转换成字符

  4. 直接创建

    ​ String str4 = “Hello”;

package String;

public class create {
    public static void main(String[] args) {
        //常见创建字符串的4种方式

        //第一种:使用空参数创建一个String
        String str1 = new String();
        System.out.println("The first String is >" + str1+ "<");

        //第二种:根据字符数组创建一个String
        char[] charArray = {'G', 'o', 'o', 'd'};
        String str2 = new String(charArray);
        System.out.println("The second String is >" + str2+ "<");

        //第三种:根据字节数组创建一个String
        byte[] byteArray = {97,98,99};  //该数字会根据ASCii表转换成字符
        String str3 = new String(byteArray);
        System.out.println("The third String is >" + str3+ "<");

        //直接创建
        String str4 = "Hello";
        System.out.println("The fourth String is >" + str4+ "<");
     
    }
}

2.字符串常量池

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

字符串常量池在堆当中。

对于基本类型来说 " == "是**数值**的比较

对于引用类型来说 " == "是**地址值**的比较

在这里插入图片描述

String API

1.equals方法(比较方法)

格式:public boolean equals (Object obj);

参数可以是任何对象,只有一个参数是字符串并且内容相同的才会返回true,否则返回false。(大小写区分)

注意
  1. 任何对象都可以用Object接收;

  2. equals方法具有对称性,a.equals(b) = b.equals(a)

  3. 若比较双方是一个常量一个变量,推荐**将常量字符串写在前面**,这样在某些情况可以避免空指针错误

    推荐:“abc”.equals(str)

    不推荐:str.equals(“abc”)

  4. equals方法严格区分大小写,equalsIgnoreCase可以不区分大小写进行比较

package String;

public class API_equals {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hello";
        char[] charArray = {'H', 'e', 'l', 'l', 'o'};
        String str3 = new String(charArray);

        System.out.println("str1.equals(str2) = " + str1.equals(str2));
        System.out.println("str2.equals(str1) = " + str2.equals(str1));
        System.out.println("str3.equals(\"Hello\") = " + str3.equals("Hello"));
        System.out.println("\"Hello\".equals(str1) = " + "Hello".equals(str1));

        String str4 = "hello";
        System.out.println("str4.equals(str1) = " + str4.equals(str1));

        System.out.println("str1.equalsIgnoreCase(str4) = " + str1.equalsIgnoreCase(str4));
    }
}

2.获取信息方法

  1. 获取长度信息方法

    ​ int length = str.length();

  2. 查找字符串方法
    1. charAt方法,获取指定索引位置的单个字符

    2. indexOf方法,该方法返回参数字符串在指定的字符串中首次出现的索引位置

      从字符串起始开始向后搜索,如果没有找到返回-1

    3. lastIndexOf方法,该方法返回参数字符串在指定的字符串中最后一次出现的索引位置

      从字符串末尾开始向前搜索,如果没有找到返回-1

package String;

public class API_infoGet {
    public static void main(String[] args) {
        String str = new String("Post&Telecommunication");
        System.out.println("String str is \"" + str + "\"" );

        //获取长度信息
        int length = str.length();
        System.out.println("String str length is " + length);

        //查找字符串方法
        //charAt方法,获取指定索引位置的单个字符
        char ch = "Hello".charAt(1);
        System.out.println("索引位置为1的字符为" + ch);

        //indexOf方法,该方法返回参数字符串在指定的字符串中首次出现的索引位置
        //从字符串起始开始向后搜索,如果没有找到返回-1
        int num1 = str.indexOf("a");
        System.out.println("str.indexOf(\"a\") = " + num1);
        int num2 = str.indexOf("com");
        System.out.println("str.indexOf(\"com\") = " + num2);

        //lastIndexOf方法,该方法返回参数字符串在指定的字符串中最后一次出现的索引位置
        //从字符串末尾开始向前搜索,如果没有找到返回-1
        int num3 = str.lastIndexOf("e");
        System.out.println("str.lastIndexOf(\"e\") = " + num3);
    }
}

3.连接字符串方法

可直接用”+“号或concat方法

concat方法

格式:str = str1.concat(str2);

package String;

public class API_combination {
    public static void main(String[] args) {
        //直接+号连接
        String a = new String("I");
        String b = new String("Love");
        String c = new String("You.");
        String str = a+ ' '+ b+ ' '+ c;
        System.out.println(str);

        //concat拼接方法
        String str1 = "Hello";
        String str2 = "World";
        String str3 = str1.concat(str2);
        String str4 = str1.concat(str);
        System.out.println(str3);
        System.out.println(str4);
    }
}

4.截取方法substring

substring有两种重载形式

public String substring(int index);

public String substring(int begin, int end);

在第二种形式当中,[begin, end),为左闭右开。

package String;

public class API_substring {
    public static void main(String[] args) {
        String str1 = "HelloWorld";
        String str2 = str1.substring(5);
        System.out.println("The substring(5) is " + str2);

        String str3 = str1.substring(4, 7);
        System.out.println("substring(4, 7)" + str3);
        
    }
}

5.转换方法

  1. toCharArray方法将当前的字符串拆分成为字符数组作为返回值。

    public char[] toCharArray();

  2. getBytes方法获得当前字符串底层的字节数组

    public byte[] getBytes();

  3. replace方法将所有出现的老字符替换成为新字符,返回替换后的结果新字符串

    public String replace(CharSequence oldString, CharSequence newString);

package String;

public class API_convert {
    public static void main(String[] args) {
        //转换成字符数组
        char[] chars = "Hello".toCharArray();
        System.out.println(chars);
        System.out.println("chars.length = " + chars.length);

        //转换成字节数组
        byte[] bytes = "ABC".getBytes();
        System.out.println("bytes.length = " + bytes.length);
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }

        //字符串的内容替换
        String str = "How do you like me?";
        String str1 = str.replace('o', '$');
        System.out.println(str);
        System.out.println(str1);
    }
}

6.转换方法split

split方法:按照参数规则将字符串切分为若干部分

public String[] split (String regex);

split的参数其实是一个”正则表达式“,如果要使用英文句点为分割点,不能直接用

String[] array = str.split(".");

这样出来数组是空的,必须使用如下格式才能正常操作

String[] array1 = str.split("\\.");

code:

package String;

public class API_split {
    public static void main(String[] args) {
        String str1 = "aba,bab,cab";
        String[] array1 = str1.split(",");
        for (int i = 0; i < array1.length; i++) {
            System.out.println(">" + array1[i] + "<");
        }

        String[] array2 = str1.split("a");
        for (int i = 0; i < array2.length; i++) {
            System.out.println(">" + array2[i] + "<");
        }

    }
}

练习

1.按指定格式拼接

定义一个方法,把数组{1,2,3}按照指定格式拼接成一个字符串。

格式:[word1#word2#word3]。

package String;

//Q:按指定格式拼接
//定义一个方法,把数组{1,2,3}按照指定格式拼接成一个字符串。
//格式:[word1#word2#word3]
public class Demo01Practice {
    public static void main(String[] args) {
        String str = "[";
        String word = "word";
        for (int i = 0; i < 3; i++) {
            if (i != 2)
                str += word + (i+1) + "#";
            else
                str += word + (i+1);
        }
        str += "]";
        System.out.println(str);
    }
}

2.统计字符串

键盘输入一个字符串,并且按种类统计其中各种字符出现的次数。

种类有大写字母,小写字母,数字以及其他

package String;

import java.util.ArrayList;
import java.util.Scanner;

public class Demo02Count {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Character> bs = new ArrayList<>();
        char [] a = new char[100];
        int upper = 0;      //大写字母
        int lower = 0;      //小写字母
        int num = 0;        //数字
        int other = 0;      //其他

        System.out.println("Please input string:");
        String str = sc.next();
        a = str.toCharArray();

        for (int i = 0; i < a.length; i++) {
            if(a[i] >= 'A' && a[i] <= 'Z')
                upper++;
            else if(a[i] >= 'a' && a[i] <= 'z')
                lower++;
            else if(a[i] >= '0' && a[i] <= '9')
                num++;
            else
                other++;
        }
        System.out.println("The number of upper Character is " + upper);
        System.out.println("The number of lower Character is " + lower);
        System.out.println("The number of number is " + num);
        System.out.println("The number of other Character is " + other);
    }
}

‘9’)
num++;
else
other++;
}
System.out.println("The number of upper Character is " + upper);
System.out.println("The number of lower Character is " + lower);
System.out.println("The number of number is " + num);
System.out.println("The number of other Character is " + other);
}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值