zoukankan      html  css  js  c++  java
  • Java中String的常用方法总结

    Java中String的常用方法总结

    1、length()字符串的长度

    1  String str="HelloWord";
    2  System.out.println(str.length());

    输出结果是10

    2、charAt() 截取一个字符

    3 getchars()截取多个字符并由其他字符串接收

    4 getBytes()将字符串变成一个byte数组

    5 toCharArray()将字符串变成一个字符数组

    6 equals()和equalsIgnoreCase()比较两个字符串是否相等,前者区分大小写,后者不区分

     

     7 startsWith()和endsWith()判断字符串是不是以特定的字符开头或结束

    8 toUpperCase()和toLowerCase()将字符串转换为大写或小写

    9 concat() 连接两个字符串

    10 trim()去掉起始和结束的空格

    11 substring()截取字符串

    12 indexOf()和lastIndexOf()前者是查找字符或字符串第一次出现的地方,后者是查找字符或字符串最后一次出现的地方

    13 compareTo()和compareToIgnoreCase()按字典顺序比较两个字符串的大小,前者区分大小写,后者不区分

    14 replace() 替换

    代码演示:

     1 package com.aaa.demo9;
     2 
     3 public class StringDemo {
     4     public static void main(String[] args) {
     5         
     6         // 2、charAr() 截取一个字符
     7         String charAtDemo="hello";
     8         System.out.println(charAtDemo.charAt(2));
     9         //输出的是l
    10         
    11         //3 getchars()截取多个字符并由其他字符串接收
    12         String getChars="hello";
    13         char[] getCharDemo=new char[10];
    14         getChars.getChars(0, 3, getCharDemo, 0);
    15         //第一个数值代表的是截取的字符串开始的位置
    16         //第二个数值代表截取要截取的字符串的结束后的下一个下标(也可以理解为截取的长度)
    17         //第三个代表的是接收的字符串数组,最后一个参数是接收数组的开始位置。
    18         System.out.println(getCharDemo);
    19         
    20         //4 getBytes()将字符串变成一个byte数组
    21         String getByte="hello";
    22         byte[] b=getByte.getBytes();
    23         System.out.println(new String(b));
    24         
    25         
    26         //5 toCharArray()将字符串变成一个字符数组
    27         String charArr="hello";
    28         char[] c = charArr.toCharArray();
    29         System.out.println(c);
    30         
    31         //6 equals()和equalsIgnoreCase()比较两个字符串是否相等,前者区分大小写,后者不区分
    32         String equalA="hello";
    33         String equalB="hello";
    34         boolean equals = equalA.equals(equalB);
    35         System.out.println(equals);
    36         //结果为true
    37         
    38         //7 startsWith()和endsWith()判断字符串是不是以特定的字符开头或结束
    39         String startA="hello.java";
    40         boolean endsWith = startA.endsWith(".java");
    41         System.out.println(endsWith);
    42         //结果为true
    43         
    44         //8 toUpperCase()和toLowerCase()将字符串转换为大写或小写
    45         String upperCase="Hllo";
    46         String upp = upperCase.toUpperCase();
    47         System.out.println(upp);
    48         //输出结果为HLLO
    49         
    50         //9 concat() 连接两个字符串
    51         String concatA="hello";
    52         String concatB="你好";
    53         String concat = concatA.concat(concatB);
    54         System.out.println(concat);
    55         
    56         //10 trim()去掉起始和结束的空格
    57         String trimA="    hello    ";
    58         System.out.println(trimA.trim());
    59         
    60         //11 substring()截取字符串
    61         String subA="hello";
    62         System.out.println(subA.substring(1));
    63         
    64         //12 indexOf()和lastIndexOf()前者是查找字符或字符串第一次出现的地方,后者是查找字符或字符串最后一次出现的地方
    65         String indexA="hello";
    66         System.out.println("index"+indexA.indexOf("e"));//返回的是下标的值,如果找不到返回-1
    67         
    68         //13 compareTo()和compareToIgnoreCase()按字典顺序比较两个字符串的大小,前者区分大小写,后者不区分
    69         String comA="hello";
    70         String comB="Word";
    71         System.out.println(comA.compareTo(comB));
    72         
    73         //14 replace() 替换
    74         String reA="hello";
    75         reA=reA.replace("e", "a");
    76         System.out.println(reA);
    77         
    78     }
    79 }
  • 相关阅读:
    ueditor内容带格式回显(html字符串回显)
    thymleaf th:text="|第${user.courseSort}课|" 对于不知道的真的是解渴了
    Thymleaf 从某处(不包含某处)开始截取字符串到末尾
    layUI 实现自定义弹窗
    layUI实现可选项 弹框
    layUI弹出框提示
    点击编辑table变为可编辑状态
    POI导出数据以Excel的方式录入,下载
    基于BootStrap的initupload()实现Excel上传和获取excel中的数据
    下载导入模板
  • 原文地址:https://www.cnblogs.com/yanpingping/p/10585676.html
Copyright © 2011-2022 走看看