<
>

JavaString类常用方法梳理总结

2022-06-14 15:49:47 来源:易采站长站 作者:

目录
一、String类概述概述特点二、使用步骤三、常用方法判断功能的方法获取功能的方法转换功能的方法分割功能的方法

一、String类概述

概述

java.lang.String>

特点

字符串不变:字符串的值在创建后不能被更改。

String s1 = "abc";
s1 += "d";
System.out.println(s1); // "abcd"
// 内存中有"abc","abcd"两个对象,s1从指向"abc",改变指向,指向了"abcd"。

因为String对象是不可变的,所以它们可以被共享。

String s1 = "abc";
String s2 = "abc";
// 内存中只有一个"abc"对象被创建,同时被s1和s2共享。

“abc” 等效于 char[] data={ ‘a’ , ‘b’ , ‘c’ } 。

例如:

String str = "abc";

相当于:

char data[] = {'a', 'b', 'c'};
String str = new String(data);
// String底层是靠字符数组实现的。

二、使用步骤

查看类:

java.lang.String>

查看构造方法:

    public String() :初始化新创建的 String对象,以使其表示空字符序列。public String(char[] value) :通过当前参数中的字符数组来构造新的String。public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String。

    构造举例,代码如下:

    // 无参构造
    String str = new String();
    // 通过字符数组构造
    char chars[] = {'a', 'b', 'c'};
    String str2 = new String(chars);
    // 通过字节数组构造
    byte bytes[] = { 97, 98, 99 };
    String str3 = new String(bytes);

    三、常用方法

    判断功能的方法

      public>public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小写。

      方法演示,代码如下:

      public class String_Demo01 {
          public static void main(String[] args) {
              // 创建字符串对象
              String s1 = "zjq";
              String s2 = "zjq";
              String s3 = "ZJQ";
              // boolean equals(Object obj):比较字符串的内容是否相同
              System.out.println(s1.equals(s2)); // true
              System.out.println(s1.equals(s3)); // false
              System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
              //boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
              System.out.println(s1.equalsIgnoreCase(s2)); // true
              System.out.println(s1.equalsIgnoreCase(s3)); // true
              System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
          }
      }

      Object 是” 对象”的意思,也是一种引用类型。作为参数类型,表示任意对象都可以传递到方法中。

      获取功能的方法

        public>public String concat (String str) :将指定的字符串连接到该字符串的末尾。public char charAt (int index) :返回指定索引处的 char值。public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。

        方法演示,代码如下:

        public class String_Demo02 {
            public static void main(String[] args) {
                //创建字符串对象
                String s = "helloworld";
                // int length():获取字符串的长度,其实也就是字符个数
                System.out.println(s.length());
                System.out.println("‐‐‐‐‐‐‐‐");
                // String concat (String str):将将指定的字符串连接到该字符串的末尾.
                String s = "helloworld";
                String s2 = s.concat(" zjq666");
                System.out.println(s2);// helloworld  zjq666
                // char charAt(int index):获取指定索引处的字符
                System.out.println(s.charAt(0));
                System.out.println(s.charAt(1));
                System.out.println("‐‐‐‐‐‐‐‐");
                // int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回‐1
                System.out.println(s.indexOf("l"));
                System.out.println(s.indexOf("owo"));
                System.out.println(s.indexOf("zhanj"));
                System.out.println("‐‐‐‐‐‐‐‐");
                // String substring(int start):从start开始截取字符串到字符串结尾
                System.out.println(s.substring(0));
                System.out.println(s.substring(5));
                System.out.println("‐‐‐‐‐‐‐‐");
                // String substring(int start,int end):从start到end截取字符串。含start,不含end。
                System.out.println(s.substring(0, s.length()));
                System.out.println(s.substring(3,8));
            }
        }

        转换功能的方法

          public>public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使用replacement字符串替换。

          方法演示,代码如下:

              public static void main(String[] args) {
                  // 转换成为字符数组
                  char[] chars = "Hello".toCharArray();
                  System.out.println(chars[0]); // H
                  System.out.println(chars.length); // 5
                  System.out.println("==============");
          
                  // 转换成为字节数组
                  byte[] bytes = "abc".getBytes();
                  for (int i = 0; i < bytes.length; i++) {
                      System.out.println(bytes[i]);
                  }
                  System.out.println("==============");
          
                  // 字符串的内容替换
                  String str1 = "How do you do?";
                  String str2 = str1.replace("o", "*");
                  System.out.println(str1); // How do you do?
                  System.out.println(str2); // H*w d* y*u d*?
                  System.out.println("==============");
          
                  String lang1 = "会不会玩儿呀!你大爷的!你大爷的!你大爷的!!!";
                  String lang2 = lang1.replace("你大爷的", "****");
                  System.out.println(lang2); // 会不会玩儿呀!****!****!****!!!
              }

          CharSequence 是一个接口,也是一种引用类型。作为参数类型,可以把String对象传递到方法中。

          分割功能的方法

          public>

          方法演示,代码如下:

          public static void main(String[] args) {
                  String str1 = "aaa,bbb,ccc";
                  String[] array1 = str1.split(",");
                  for (int i = 0; i < array1.length; i++) {
                      System.out.println(array1[i]);
                  }
              }

          到此这篇关于Java String类常用方法梳理总结的文章就介绍到这了,更多相关Java String类 内容请搜索易采站长站以前的文章或继续浏览下面的相关文章希望大家以后多多支持易采站长站!

暂时禁止评论

微信扫一扫

易采站长站微信账号