• 2019-06-3 java学习日记


    String类的获取功能:

    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):从指定位置开始到指定位置结束截取字符串、

    例子:

     1 public class Demo5_StringMethod {
     2 public static void main(String[] args) {
     3         String s = "woaiheima";
     4         s.substring(4);                                            
     5        //subString会产生一个新额字符串,需要将新的字符串记录
     6         System.out.println(s);
     7     }
     8 
     9     private static void demo4() {
    10         String s1 = "heimawudi";
    11         String s2 = s1.substring(5);
    12         System.out.println(s2);
    13         
    14         String s3 = s1.substring(0, 5);        //包含头,不包含尾,左闭右开
    15         System.out.println(s3);
    16     }
    17 
    18     private static void demo3() {
    19         String s1 = "woaiheima";
    20         int index1 = s1.indexOf('a', 3);        //从指定位置开始向后找
    21         System.out.println(index1);
    22         
    23         int index2 = s1.lastIndexOf('a');        //从后向前找,第一次出现的字符
    24         System.out.println(index2);
    25         
    26         int index3 = s1.lastIndexOf('a', 7);    //从指定位置向前找
    27         System.out.println(index3);
    28     }
    29 
    30     private static void demo2() {
    31         String s1 = "heima";
    32         int index = s1.indexOf('e');            
    33     //参数接收的是int类型的,传递char类型的会自动提升
    34         System.out.println(index);
    35         
    36         int index2 = s1.indexOf('z');        //如果不存在返回就是-1
    37         System.out.println(index2);
    38         
    39         int index3 = s1.indexOf("ma");    //获取字符串中第一个字符出现的位置
    40         System.out.println(index3);
    41         
    42         int index4 = s1.indexOf("ia");
    43         System.out.println(index4);
    44     }
    45 
    46     private static void demo1() {
    47         //int[] arr = {11,22,33};
    48         //System.out.println(arr.length);        //数组中的length是属性
    49         String s1 = "heima";
    50         System.out.println(s1.length());    
    51     //length()是一个方法,获取的是每一个字符的个数
    52         String s2 = "你要减肥,造吗?";
    53         System.out.println(s2.length());
    54         
    55         char c = s2.charAt(5);    //根据索引获取对应位置的字符
    56         System.out.println(c);
    57         char c2 = s2.charAt(10);                                    
    58       //StringIndexOutOfBoundsException字符串索引越界异常
    59         System.out.println(c2);
    60     }
    61 
    62 }

    String类的转换功能:

    1,byte[ ] getBytes():把字符串转换为字节数组

    private static void demo1() {
            String s1 = "abc";
            byte[] arr = s1.getBytes();
            for (int i = 0; i < arr.length; i++) {
                //System.out.print(arr[i] + " ");
            }
            
            String s2 = "你好你好";
            byte[] arr2 = s2.getBytes();      //通过gbk码表将字符串转换成字节数组
    for (int i = 0; i < arr2.length; i++) {   //编码:把我们看的懂转换为计算机看的懂得
        //System.out.print(arr2[i] + " ");    //gbk码表一个中文代表两个字节
            }                            //gbk码表特点,中文的第一个字节肯定是负数
            
            String s3 = "琲";
            byte[] arr3 = s3.getBytes();
            for (int i = 0; i < arr3.length; i++) {
                System.out.print(arr3[i] + " ");
            }
        }

    2,char[ ] toCharArray():把字符串转换为字符数组

    1 private static void demo2() {
    2         String s = "heima";
    3         char[] arr = s.toCharArray();            //将字符串转换为字符数组
    4         
    5         for (int i = 0; i < arr.length; i++) {
    6             System.out.print(arr[i] + " ");
    7         }
    8     }

    3,static String valueOf(char[ ] chs):把字符数组转成字符串

         static String valueOf(int i):把int类型的数据转成字符串

    注意:String类的valueOf方法可以把任意类型的数据转成字符串

     1 private static void demo3() {
     2         char[] arr = {'a','b','c'};
     3         String s = String.valueOf(arr);    //底层是由String类的构造方法完成的
     4         System.out.println(s);
     5         
     6         String s2 = String.valueOf(100);        //将100转换为字符串
     7         System.out.println(s2 + 100);
     8         
     9         Person p1 = new Person("张三", 23);
    10         System.out.println(p1);
    11         String s3 = String.valueOf(p1);        //调用的是对象的toString方法
    12         System.out.println(s3);
    13     }

    4,String toLowerCase():把字符串转成小写

       String toUpperCase():把字符串转成大写

         String concat(String str):把字符串拼接

     1 public static void main(String[] args) {
     2         String s1 = "heiMA";
     3         String s2 = "chengxuYUAN";
     4         String s3 = s1.toLowerCase();
     5         String s4 = s2.toUpperCase();
     6         
     7         System.out.println(s3);
     8         System.out.println(s4);
     9         
    10         System.out.println(s3 + s4);          //用+拼接字符串更强大,可以用字符串与任意类型相加
    11         System.out.println(s3.concat(s4));    //concat方法调用的和传入的都必须是字符串
    12     }

     

  • 相关阅读:
    search
    longestValidParentheses
    nextPermutation
    linux下通过安装xampp搭建环境卸载xampp、以及网站搬家-64位系统Apache启动不了的问题
    对拍器 ADC-ACMDataCreater
    Educational Codeforces Round 86 (Rated for Div. 2) A~D
    Codeforces Round #637 (Div. 2) A~D
    Codeforces Round #636 (Div. 3) A~D
    Codeforces Round #635 (Div. 2) A~D
    原型设计工具比较及实践 基于WolframAlphaAPI的科学计算器原型设计
  • 原文地址:https://www.cnblogs.com/Sherwin-liao/p/10970382.html
Copyright © 2020-2023  润新知