Java的String类

发布于:2022-12-17 ⋅ 阅读:(155) ⋅ 点赞:(0)

认识String类

在Java中 String 是属于引用数据类型,用 String 类创建的变量不会存储字符串本身,而是存放字符串所在的地址
String 类的内容是使用用 “ ”引用的,其" " 里的内容是属于常量
String 类是在 java.lang 包底下,里面包含了字符串的值和实现字符串相关操作的一些方法
String 类是不能被继承的,因为它是被 final 所修饰

在 String 类的源码里一个对象都有二个成员变量,分别是:value[ ]hash
value[ ] 是一个数组类型

在这里插入图片描述

String类常用方法

String 类 提供的构造方法非常多,但常用的就下面三种:

public static void main(String[] args) {
        // 使用常量字符串创建
        String n = "hello";
        System.out.println(n);

        // 直接newString对象
        String n2 = new String("abc");
        System.out.println(n2);

        //使用字符数组进行构造
        char[] arr = {'c','h','i','n','a'};
        String n3 = new String(arr);//把字符数组装化成字符串
        System.out.println(n3);
    }

在 Java 求字符串长度是使用对象 . length()
例如:

 System.out.println(n.length());
 System.out.println("hello".length());

字符串对象的比较

字符串是属于引用类型,不能使用 == 来判断是否相等
例如:

public static void main(String[] args) {
       // 对于基本类型,可以使用 == 来判断二个变量是否相等
        int a = 10;
        int b = 10;
        System.out.println(a == b);  

        // 对于引用类型变量,是不能使用 == 比较两个引用变量,它们比较的是地址,不是内容
        String array1 = new String("hello");
        String array2 = new String("hello");
        String n = new String("world");
        n = array1;

        System.out.println(array1 == array2);
        System.out.println(n == array1);

  }

结果:
在这里插入图片描述

字符串比较是否相等:

比较相等是使用 equals 来比较内容
String类里重写了父类Object中equals方法,Object中的 equals默认按照==比较
String重写equals方法后,按照内容进行比较

public static void main(String[] args) {
      String array1 = new String("hello");
      String array2 = new String("hello");
      System.out.println(array1.equals(array2));
 } 
字符串比较大小

比较二个字符串的大小使用的是compareTo
compareTo 的返回值是整数,需要用整形变量来接收
返回大于1 的数字代表 变量1 大于 变量2
返回0 代表 二个字符串相等
返回小于0 代表 变量1 小于 变量2
例如:

public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        int ret = s1.compareTo(s2);
        if(ret > 0){
            System.out.println("s1 > s2");
        } else if (s1 == s2) {
            System.out.println("s1 == s2");
        }else {
            System.out.println("s1 < s2");
        }
    }
字符串忽略大小写比较大小

使用equalsIgnoreCase忽略字符串的大小写判断是否相同

public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("Hello");
        System.out.println(s1.equalsIgnoreCase(s2));
 }

结果:

在这里插入图片描述

使用compareToIgnoreCase 是忽略字符串的大小写,进行比较

public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("ello");
        int ret = s1.compareToIgnoreCase(s2);
        if(ret > 0){
            System.out.println("s1 > s2");
        } else if (s1 == s2) {
            System.out.println("s1 == s2");
        }else {
            System.out.println("s1 < s2");
        }
    }

字符串查找

方法 功能
charAt(int index 返回index位置上字符,如果index 是负数或者越界,会出现异常
int indexOf(int ch 返回返回ch第一次出现的位置,没有返回-1
int indexOf(int ch, int fromIndex fromIndex位置开始找ch第一次出现的位置,没有返回-1
int indexOf(String str 返回str第一次出现的位置,没有返回-1
int indexOf(String str, int fromIndex fromIndex位置开始找str第一次出现的位置,没有返回-1
int lastIndexOf(int ch 从后往前找,返回ch第一次出现的位置,没有返回-1
int lastIndexOf(int ch, int fromIndex fromIndex位置开始找,从后往前找 ch第一次出现的位置,没有返回-1
int lastIndex(String str 从后往前找,返回str第一次出现的位置,没有返回-1
int lastIndexOf(String str, int fromIndex fromIndex 位置开始找,从后往前找 str第一次出现的位置,没有返回-1

使用方法例子:

charAt 使用

charAt 是从字符串中获取一个字符

public static void main(String[] args) {
        String n = "ababcdefgabc";
        // n.length 是计算字符串长度
        for (int i = 0; i < n.length(); i++) {
            // charAt 是从字符串中获取一个字符
            char ch = n.charAt(i);
            System.out.println(ch);
        }
    }
indexof 使用

indexof 有四种重载的方法:

第一种:是从头开始找一个字符,遇到遇到要找的字符就结束
第二种:是自定义从哪开始往后找,遇到遇到要找的字符就结束
第三种:从字符串中找一个字符串,找到返回字符串起始的下标
第四种:从字符串中找一个字符串,自定义从哪开始往后找,找到返回字符串起始的下标

public static void main(String[] args) {
        String n = "ababcdefgabc";

        System.out.println(n.indexOf('c'));  // c 的下标是:4
        System.out.println(n.indexOf('b',2)); // 从下标为2开始找 b,b的下标是:3 
        System.out.println(n.indexOf("abc")); // 如果找到返回要找字符串的起始下标
        System.out.println(n.indexOf("abc",4)); //从下标为 4 开始往后找
          
lastIndexOf 使用

lastIndexOf 也有四种重载方法:
第一种:是从字符串最后面往前找一个字符,找到直接返回下标
第二种:自定义从哪开始往前找,找到直接返回下标
第三种:从字符串中找一个字符串,是从最后面开始往前找,找到返回下标
第四种:自定义从哪开始往前找一个字符串,找到返回字符串起始的下标

public static void main(String[] args) {
        String n = "ababcdefgabc";
        System.out.println(n.lastIndexOf('c')); //从字符串最后面找一个字符 ‘c’,找到返回它的下标
        System.out.println(n.lastIndexOf('c',5)); // 从字符串下标元素为 5 的字符开始往后找
        System.out.println(n.lastIndexOf("abc"));
        System.out.println(n.lastIndexOf("abc",5));

字符串转化

字符串和数值转换

整数转字符串使用:类型. valueOf
字符串转数值使用:类型 . parseDouble
以上二种都可以互相使用

   public static void main(String[] args) {
        // 数值转字符串
        String s1 = String.valueOf(1234);
        String s2 = String.valueOf(3.15);
         System.out.println(s1);
         System.out.println(s2);
         // 字符串转数值
          int ret = Integer.parseInt("123");
        int ret2 = Integer.valueOf("123"); 
        System.out.println(ret+1);
        System.out.println(ret2+1);

        double ret3 = Double.parseDouble("4.68");
        double ret4 = Double.valueOf("4.68");
        System.out.println(ret3);
        System.out.println(ret4);
    }
字符串大小写的转换

字符串小写转换大写使用:变量名 . toUpperCase()
字符串大写转换小写使用:变量名 .toLowerCase()

public static void main(String[] args) {
        //小写转大写
        String s1 = "hello";
        String n = s1.toUpperCase();
        System.out.println(n);
       //大写转想小写
        String s2 = "HELLO";
        String m = s2.toLowerCase();
        System.out.println(m);
    }
字符串转数组
 public static void main(String[] args) {
        // 字符串转数组
        String s1 = "ABCDE";
        char[] n = s1.toCharArray();
        // 打印
        System.out.println(Arrays.toString(n));

        //数组转字符串
        char[] m = {'A','B','C'};
        String s2 = new String(m);
        System.out.println(s2);
    }

结果:

在这里插入图片描述

格式化

格式化使用:format

 public static void main(String[] args) {
        String s = String.format("%d-%d-%d", 2009, 9,14);
        System.out.println(s);
    }

字符串替换

字符串替换使用:变量名 . replace
replace 有二种方法重载:
第一种:替换二个字符
第二种:替换一个字符
在这里插入图片描述

public static void main(String[] args) {
        String n = "abcabcabc";
        // 把字符串里的 a 改成 o
        String ret = n.replace('a','o');
        System.out.println(ret);
        // 把字符串里的 ab 改成 og
        String ret2 = n.replace("ab","og");
        System.out.println(ret2);
    }

replaceAll 是把字符串里的某个字符串改为另一个字符串,字符串可多可少
replaceFirst 是把第一个 ab 改成 gtr

 public static void main(String[] args) {
        String n = "abcabcabc";
        // 把字符串里的 ab 改成 gtr
        String ret2 = n.replaceAll("ab","gtr");
        System.out.println(ret2);

        String ret2 = n.replaceFirst("ab","gtr");
        System.out.println(ret2);
        

字符串拆分

可以把一个完整的字符串按照指定的分割符划分为若干个字符串
需要使用:变量名 . split(),接收要用字符数组
二种使用方法:
第一种:全部截取
第二种:自定义截取多少次

  public static void main(String[] args) {
        String n = "xiaoming&wangwu&zhangsan";
        //全部截取
        String[] ret = n.split("&");
        // 自定义截取
        String[] ret2 = n.split("&",2);

        System.out.println(Arrays.toString(ret));
        System.out.println(Arrays.toString(ret2));
    }

结果:
在这里插入图片描述

拆分是特别常用的操作. 一定要重点掌握. 另外有些特殊字符作为分割符可能无法正确切分, 需要加上转义

例如:

 public static void main(String[] args) {
        String str = "192.168.1.1";
        String[] result = str.split("\\.");
        for (String s : result) {
            System.out.println(s);
        }
    }
  • 如果字符| * + .都得加上转义字符,前面加上\\
  • 如果是 \,那么就得写成 \\\\
  • 如果一个字符串中有多个分隔符,可以用"|"作为连字符
String[] ret = str.split("& | .");

多次拆分
截取二次

 public static void main(String[] args) {
        String str = "naeme=zhangsan&age=18";
        String[] r = str.split("&");
        for (String s : r) { // 把 r 里名截取的字符串传给s  name=zhangsan  age=18
           String[] std = s.split("=");// 再以 = 截取字符串
           for (String n : std){
               System.out.println(n);
           }
        }
    }

字符串截取

从一个完整的字符串之中截取出部分内容
字符串截取使用 变量名.substring( )
例如:

 public static void main(String[] args) {
        String n = "ABCDEF";
        // 截取 下标为 1 往后的字符串
        String s1 = n.substring(1);
        // 截取 1 到 4-1 之间的字符串
        String s2 = n.substring(1,4);
        System.out.println(s1);
        System.out.println(s2);
    }

结果:

在这里插入图片描述

观察trim()方法的使用
例如:
trim 会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等)

public static void main(String[] args) {
     String str = " hello world " ;
     String n = str.trim();
     System.out.println(n);
 }

网站公告

今日签到

点亮在社区的每一天
去签到