zoukankan      html  css  js  c++  java
  • 27 String类

    27 String类

    底层实现

    ​ final的char数组

    常用的加强版

    StringBuffer

    特点:线程安全、速度慢

    StringBuilder

    特点:线程不安全、速度快

    出现的原因:解决String自行拼接以及反序麻烦等问题

    加强版与String类型的转换

    对象名.toString();------------//大部分类型的数据转String类的方法都是toString()方法

    StringBuffer stringBuffer=new StringBuffer();
    String str=stringBuffer.toString();
    

    String的常用方法

    前提:

    对字符串进行任意修改,返回的是一个新字符串,原来的字符串保持不变

    若是对字符串进行判断,则返回的是布尔类型

    1.根据下标找对应的值

    字符串名称.indexof(int ch);
    //返回指定字符在此字符串中第一次出现的索引
    字符串名称.indexof(int ch,fromIndex);
    //从指定的索引起,返回指定字符在此字符串中第一次出现的索引
    //传的实参数值的是字符串
    字符串名称.indexof(String str);
    //返回指定子字符串在此字符串中第一次出现的索引
    字符串名称.indexof(int ch,fromIndex);
    //从指定的索引起(含起步的位置),返回指定子字符串在此字符串中第一次出现的索引(首字母)
    若为找到就都是返回-1
    
    //示例demo
    public class String01 {
        public static void main(String[] args) {
            String name="The tree is beautiful,The tree is green!!!";
            System.out.println("字符t出现的首次位置为:"+name.indexOf('t'));
            System.out.println("从第6个"+"字符起,t出现的首次位置为:"+name.indexOf('t',5));
            System.out.println("从第5个"+"字符起,t出现的首次位置为:"+name.indexOf('t',4));
            //查找子字符串的位置
            System.out.println("子字符串出现的首次位置的第一个元素的地址为:"+name.indexOf("tree"));
            System.out.println("从第6个"+"字符起,t出现的首次位置的第一个元素的地址为:"+name.indexOf("tree",5));
            System.out.println("从第5个"+"字符起,t出现的首次位置的第一个元素的地址为:"+name.indexOf("tree",4));
        }
    }
    //页面xia
    

    2.获取对应下标的字符

     String name = "The tree is beautiful,The tree is green!!!";
    //字符串.charAt(int index);
    System.out.println(name.charAt(1));
    //----------获取的为h---------
    

    3.获取字符串长度

     String name = "The tree is beautiful,The tree is green!!!";
     System.out.println(name.length());
    

    4.截取字符串(2个)

    字符串.subString(int beginIndex);
    //获取从beginIndex开始到结尾的子字符串
    字符串.subString(int beginIndex,int endIndex);
    //获取从beginIndex(含)开始到endIndex(不含结尾)的子字符串
    String name = "The tree is beautiful,The tree is green!!!";
    String name = "The tree is beautiful,The tree is green!!!";       System.out.println(name.substring(4));      System.out.println(name.substring(4,7));    System.out.println(name.substring(4,8));
    

    5.分隔字符串

    //字符串.split(String regex);
    //regex代表按照什么标准划分,会按照标准划分成多个字符串
    String name = "The tree is beautiful The tree is green!!!";
    String[] str=name.split(" ");
    for (int i = 0; i < str.length; i++) {
            System.out.println(str[i]);
         }
    //得到的是每个单词      
    

    6.替换字符串

    //字符串.replace(老字符串,新字符串);
    //得到一个新的字符串,使得原来字符串中的老字符串变成了新字符串
    String name = "The tree is beautiful,The tree is green!!!";
    String name01=name.replace("tree","forest");
    System.out.println(name01);
    System.out.println(name);
    

    7.拼接字符串(与+""类似)

    字符串.concat(String str)
    //得到一个新字符串,它为原字符串末尾加上str字符串
     String name = "The tree is beautiful,The tree is green!!!";
    String name02=name.concat("Yeah");
    System.out.println(name02);
    System.out.println(name);
    

    8.判断字符是否存在

    字符串.contains(指定字符串);
    //判断指定字符串是否存在
    String name = "The tree is beautiful,The tree is green!!!";      System.out.println(name.contains("tree"));  System.out.println(name.contains("Tree"));
    

    9.英文转换成大写的

    //字符串.toUpperCase(指定字符串);
    ////得到一个新的字符串,使得原来字符串中的字母全都变成大写
    String name = "The tree is beautiful,The tree is green!!!";
    String name03=name.toUpperCase();
    System.out.println(name03);
    System.out.println(name);
    

    10.去掉前后空格

    字符串.trim(指定字符串);
    ////得到一个新的字符串,使得原来字符串中前后空格去掉
    String name = "            The tree is beautiful,The tree is green!!!            ";
    String name04=name.trim();
    System.out.println(name);
    System.out.println(name04);
    
  • 相关阅读:
    常用的系统操作需要的响应时间
    几种RAID技术比较
    iptables详解
    mount命令详解
    解决CSocket高数据传输问题
    VC++ ComBox下拉菜单看不到值
    封装MySQL C API 基本操作
    MySQL存储过程和存储函数
    MYSQL 常用命令
    VS2005连接MySQL C API
  • 原文地址:https://www.cnblogs.com/SSSzhanglingzhi/p/14066564.html
Copyright © 2011-2022 走看看