zoukankan      html  css  js  c++  java
  • Java- String类概述

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

    String s1 = "abc"; 
    s1 += "d"; 
    System.out.println(s1); // "abcd" 
    // 内存中有"abc","abcd"两个对象,s1从指向"abc",改变指向,指向了"abcd"。
    2. 因为String对象是不可变的,所以它们可以被共享。 
    String s1 = "abc"; 
    String s2 = "abc"; 
    // 内存中只有一个"abc"对象被创建,同时被s1和s2共享。
    System.out.println(s1==s2);//true
    3. "abc" 等效于 char[] data={ 'a' , 'b' , 'c' } 。 
    // 通过字符数组构造 
    char chars[] = {'a', 'b', 'c'}; 
    String str2 = new String(chars);//abc
    //通过字节数组构造
    byte bytes[] = { 97, 98, 99 }; 
    String str3 = new String(bytes);//abc

    4.两个字符串通过equals比较

    // 创建字符串对象 
    String s1 = "hello"; 
    String s2 = "hello"; 
    String s3 = "HELLO"; 
    
    // 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("‐‐‐‐‐‐‐‐‐‐‐");
    5.获取功能的方法
    • public int length () :返回此字符串的长度。
    • 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 static void main(String[] args) {     
        //创建字符串对象 
        String s = "helloworld"; 
        
        // int length():获取字符串的长度,其实也就是字符个数 
        System.out.println(s.length()); 
        System.out.println("‐‐‐‐‐‐‐‐"); 
        
        // String concat (String str):将将指定的字符串连接到该字符串的末尾. 
        String s2 = s.concat("**hello itheima");
        System.out.println(s2);// helloworld**hello itheima 
        
        // 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")); //2
        System.out.println("owo="+s.indexOf("owo")); //owo=4 索引下标是重0开始
        System.out.println(s.indexOf("ak")); 
        System.out.println("‐‐‐‐‐‐‐‐"); 
        
        // String substring(int start):从start开始截取字符串到字符串结尾 
        System.out.println(s.substring(0)); //helloworld
        System.out.println(s.substring(5)); //world  不包含5  索引下标是重1开始 不是从0开始
        System.out.println("‐‐‐‐‐‐‐‐"); 
        
        // String substring(int start,int end):从start到end截取字符串。含start,不含end。
        System.out.println(s.substring(0, s.length())); //helloworld
        System.out.println(s.substring(3,8)); //lowor
    }
    6.转换功能的方法
    • public char[] toCharArray () :将此字符串转换为新的字符数组。
    • public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
    • public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使用replacement字符串替换
    public static void main(String[] args) { 
        //创建字符串对象 
        String s = "abcde"; 
        
        // char[] toCharArray():把字符串转换为字符数组 
        char[] chs = s.toCharArray(); 
        for(int x = 0; x < chs.length; x++) { 
            System.out.println(chs[x]); 
        }System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); 
        
        // byte[] getBytes ():把字符串转换为字节数组
        byte[] bytes = s.getBytes(); 
        for(int x = 0; x < bytes.length; x++) { 
            System.out.println(bytes[x]); 
        }
        System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); 
        
        // 替换字母it为大写IT
        String str = "itcast itheima"; 
        String replace = str.replace("it", "IT"); 
        System.out.println(replace); // ITcast ITheima 
        System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
    }
    7.分割功能的方法 
    • public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。
    public static void main(String[] args) { 
        //创建字符串对象
        String s = "aa|bb|cc"; 
        String[] strArray = s.split("|"); // ["aa","bb","cc"] 
        for(int x = 0; x < strArray.length; x++) { 
            System.out.println(strArray[x]); // aa bb cc 
        } 
    }
  • 相关阅读:
    后端PHP框架laravel学习踩的各种坑
    IE6、IE7兼容querySelectorAll和querySelector方法-最终版本
    package.json保存
    原生javascript兼容性总结
    原生javascript代码懒加载
    禁止选择文本样式
    xml转实体类的实现
    kendo ui editor 文本编辑器 自定义高度的实现
    setTimeout执行时带参数,并且带的是object对象类型的参数
    KendoUi listview template的用法记录
  • 原文地址:https://www.cnblogs.com/gjq1126-web/p/11387615.html
Copyright © 2011-2022 走看看