String常用方法【构造器】

构造器+常用方法

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * String 不可变的字符序列
 * StringBuffer 可变字符序列,线程安全,效率低
 * StringBuilder 可变字符序列,线程安全,效率高
 *
 * String:字符串类,所有双引号包括的内容都是字符串的实现("aaa")
 *
 * String str01="aa"  直接用双引号,会在字符串常量池创建,并指向常量池地址
 * String str02=new String("aaa") 1:new String()对象在堆中,2:"aaa"在常量池中
 * 指向堆中的地址
 *
 *  String底层存储字符数据的结构:
 *         jdk8 :  private final char[] value;
 *         jdk11 : private final byte[] value;
 */
public class demo002 {
    public static void main(String[] args) throws UnsupportedEncodingException {
/*
        String的构造器
 */
        //1:初始化创建String()对象
        String str01=new String();

        //2:根据""创建对象
        String str02=new String("aa"); //堆中地址
        String str03="aa"; //常量池地址
        String str04="aa"; //常量池地址
        System.out.println(str02==str03); //false
        System.out.println(str04==str03); //true

        //3:根据char数组来创建
        char[] ch = {'a','b','c','d','e'};
        String str2 = new String(ch);
//        String str2 = new String(ch,2,3);  指定数组开始下标,结束下标

        //4:getBytes()  字符串转为字节数组
        byte[] arr = "你好中国".getBytes();  //gbk  1个汉字2个字节  utf-8  3个字节一个汉字
        byte[] arr2  = "你好中国".getBytes("GBK");
        System.out.println(arr.length);
        System.out.println(Arrays.toString(arr));

         //String(byte[] bytes, int offset, int length) 通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String 。
         String str05 = new String(arr,0,1);
        //使用指定的字符集解码  构造新的String
         String str06 = new String(arr,0,1,"GBK");
         System.out.println(str05);
         System.out.println(str06);

//     常用方法
        //1:根据下表获取字符
        String str07 = "aaaaa";
        //char charAt(int index) 返回指定索引处的 char值。
        //int codePointAt(int index) 返回指定索引处的字符(Unicode代码点)。
        //int codePointBefore(int index) 返回指定索引之前的字符(Unicode代码点)。
        System.out.println(str07.charAt(5));
        System.out.println(str07.codePointAt(5));

        //2:比较字符串大小 compareTo(String anotherString) 按字典顺序比较两个字符串
        //ompareToIgnoreCase(String str) 按字典顺序比较两个字符串,忽略大小写
        //返回值等于0 相同   <0 [str06<str07]   >0 [str06>str07]
        System.out.println(str06.compareTo(str07));

        //3:拼接字符串 String concat(String str) 将指定的字符串连接到此字符串的末尾。
        System.out.println(str06.concat(str07));

        //4:boolean contains(CharSequence s) 当且仅当此字符串包含指定的char值序列时,才返回true。
        //boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结尾。
        //boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开头。
        System.out.println(str07.contains("a"));

        //5:字符串比较:equals(Object anObject) 将此字符串与指定的对象进行比较。
        //6:转换数组byte[] getBytes() 使用平台的默认字符集将此 String编码为字节序列,将结果存储到新的字节数组中。
        //byte[] getBytes(String charsetName) 使用命名的字符集将此 String编码为字节序列,将结果存储到新的字节数组中。

        //7:搜索子字符串:int indexOf(String str) 返回指定子字符串第一次出现的字符串中的索引。
        //int lastIndexOf(String str) 返回指定子字符串最后一次出现的字符串中的索引。
        System.out.println(str07.indexOf("a"));
        System.out.println(str07.lastIndexOf("a"));
        //8:String intern() 返回字符串对象的规范表示。
        //返回当前字符串对象在字符串常量池中的字符串常量-->先去常量池中判断是否存在当前字符串常量,如果存在直接返回,如果不存在,在常量值中添加一个堆中new的对象的引用,返回常量值中对象的引用
        String s = new String("abc");
        String s1 = "abc";
        String s2 = new String("a")+new String("b"); //字符串的拼接: jdk1.8中内部先构建StringBuilder,通过StringBuilder进行字符串的拼接,最后返回StringBuilder的toString()返回值,返回一个新的String对象
        s2.intern();
        String s3 = "ab";
        System.out.println(s1==s); //falserr
        System.out.println(s1==s.intern());  //true
        System.out.println(s2==s3); //true


        //9:boolean isBlank() 如果字符串只有空格
        System.out.println("".isBlank());
        System.out.println("  ".isBlank());
        System.out.println(" asd".isBlank());

        //10:boolean isEmpty() 返回 判断 length()是 0 。
        System.out.println("".isEmpty());
        System.out.println("  ".isEmpty());

        //11:int length() 返回此字符串的长度。
        System.out.println("你好".length());

        //12:String repeat(int count) 返回一个字符串,其值为此字符串的串联重复 count次。
        System.out.println("abc".repeat(3));

        //13:String replace(CharSequence target, CharSequence replacement) 将此字符串中与文字目标序列匹配的每个子字符串替换为指定的文字替换序列。
        //String replaceFirst(String regex, String replacement) 将给定替换的给定 regular expression匹配的此字符串的第一个子字符串替换。
        System.out.println(str07.replace("a","A"));
        System.out.println(str07.replaceFirst("a","A"));

        //14:String[] split(String regex) 将此字符串拆分为给定 regular expression的匹配 项 。
        System.out.println(Arrays.toString("username=zhangsan".split("=")));

        //15:“ ”全角空格   “ ”半角空格
        //String trim() 返回一个字符串,其值为此字符串,删除了所有前导和尾随空格,只针对半角空格
        //String strip() 返回一个字符串,其值为此字符串,并删除了所有前导和尾随 white space 。 半角全角都能去除
        //String stripLeading() 返回一个字符串,其值为此字符串,并删除了所有前导 white space 。  半角全角都能去除
        //String stripTrailing() 返回一个字符串,其值为此字符串,并删除所有尾随 white space 。   半角全角都能去除
        System.out.println(" nihao ".trim());
        System.out.println(" nihao ".strip());

        //16:String substring(int beginIndex) 返回一个字符串,该字符串是此字符串的子字符串。
        //String substring(int beginIndex, int endIndex) 返回一个字符串,该字符串是此字符串的子字符串。
        System.out.println(str07.substring(5));
        System.out.println(str07.substring(5,12));

        //17:char[] toCharArray() 将此字符串转换为新的字符数组。
        System.out.println(Arrays.toString(str07.toCharArray()));

        //18:String toLowerCase() 使用默认语言环境的规则将此 String所有字符转换为小写。
        //String toUpperCase() 使用默认语言环境的规则将此 String所有字符转换为大写。
        System.out.println(str07.toUpperCase());

        //19:static String valueOf(boolean b) 返回 boolean参数的字符串表示形式。
        System.out.println(String.valueOf(false));
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值