String类(常用方法2)

获取功能

  “nihao”.length()     :返回长度

  “nihao”.charAt(2)   : 返回索引为2处的

  “nihao”.indexOf('i')  :   

/*
    * int length():获取字符串的长度。
    * char charAt(int index):获取指定索引位置的字符
    * int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
    * int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
    * int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
    * int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
    * lastIndexOf
    * String substring(int start):从指定位置开始截取字符串,默认到末尾。
    * String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
     */
public static void demo4() {
        String s1 = "heimawudi";
        String s2 = s1.substring(5);
        System.out.println(s2);
        String s3 = s1.substring(0, 5); // 字符串截取  substring  取头不取尾     返回一个新的字符串
        System.out.println(s3);
    }
public class test3 {
    /*
    * 案例演示
    * 需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数,其他字符出现的次数。
    * ABCDEabcd123456!@#$%^
    * 字符有值范围,通过范围判断
     */
    public static void main(String[] args) {
        String s = "ABCDEabcd123456!@#$%^";
        int big = 0;
        int small = 0;
        int num = 0 ;
        int orther = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c>='A'&&c<='Z') {
                big++;
            } else if(c>='a'&&c<='z'){
                small++;
            } else if (c>='0'&&c<='9') {
                num++;
            }else {
                orther++;
            }
        }
        System.out.println(small+" " + big + " " + num + " " +orther);
    }

}

转换功能
  getByte() 转换成字节数组 
  toCharArray 转成字符数据 
  valueOf(char[] arr) 将 字符数组转换成字符串 底层用的也是构造方法
  valueOf(int i) 将int数 转换成字符串

  valueOf()  是静态方法,可以类名. 调用  :  可以将任意类型转换成字符串String
  toLowerCase() t   oUpperCase() 转大写小写

public class Demon6_StringMethod {
    /*
    * byte[] getBytes():把字符串转换为字节数组。
    * char[] toCharArray():把字符串转换为字符数组。
    * static String valueOf(char[] chs):把字符数组转成字符串。
    * static String valueOf(int i):把int类型的数据转成字符串。
        * 注意:String类的valueOf方法可以把任意类型的数据转成字符串

    * String toLowerCase():把字符串转成小写。
    * String toUpperCase():把字符串转成大写。
    * String concat(String str):把字符串拼接*/
    public static void main(String[] args) {
        //demo1();
        //demo2();
        Person p1 = new Person("张三",23);
        System.out.println(p1);
        String s1 = String.valueOf(p1);  // 调用的是对象的tostring方法
        System.out.println(s1);
    }

    public static void demo2() {
        char[] arr = {'a','b','c'};
        String s1 = String.valueOf(arr);  // valueof()是静态方法,直接类名调用,将字符数组转换给字符串
        System.out.println(s1);
        String s2 = String.valueOf(97);
        System.out.println(s2);
    }

    public static void demo1() {
        String s1 = "heiMA";
        String s2 = "chengxuYUAN";
        char[] arr = s1.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }

}

其他功能
  "heima".replace(old,new) 将旧的替换成新的


  trim() 去除前后空格       # 用户名之类的 用这个 


  compareTo() 按字典顺序(unicode)比较两个字符串 区分大小写, 一个一个比    先比前一个,再往后移

  conpareToIgnoreCase()   :   不区分大小写的比较

  split(regex)     : 切割,返回一个字符串数组

String s1 = "woaiHeimahhHAHHA";
String s2 = s1.substring(0, 1).toUpperCase().concat(s1.substring(1).toLowerCase());  //   首字母大写,其他字母小写
System.out.println(s2);
public class Demon6_StringMethod {
    /*
    * byte[] getBytes():把字符串转换为字节数组。
    * char[] toCharArray():把字符串转换为字符数组。
    * static String valueOf(char[] chs):把字符数组转成字符串。
    * static String valueOf(int i):把int类型的数据转成字符串。
        * 注意:String类的valueOf方法可以把任意类型的数据转成字符串

    * String toLowerCase():把字符串转成小写。
    * String toUpperCase():把字符串转成大写。
    * String concat(String str):把字符串拼接。
     */
    public static void main(String[] args) {
        //demo1();
        //demo2();
        Person p1 = new Person("张三",23);
        System.out.println(p1);
        String s1 = String.valueOf(p1);  // 调用的是对象的tostring方法
        System.out.println(s1);
    }

    public static void demo2() {
        char[] arr = {'a','b','c'};
        String s1 = String.valueOf(arr);  // valueof()是静态方法,直接类名调用,将字符数组转换给字符串
        System.out.println(s1);
        String s2 = String.valueOf(97);
        System.out.println(s2);
    }

    public static void demo1() {
        String s1 = "heiMA";
        String s2 = "chengxuYUAN";
        char[] arr = s1.toCharArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }

}
竹杖芒鞋轻胜马,一蓑烟雨任平生。 回首向来萧瑟处,也无风雨也无晴。
原文地址:https://www.cnblogs.com/yaobiluo/p/11302070.html