• 【java基础】-String类常用方法总结


    1.求字符串长度(字符个数):length()

    String str1 = "this is old str";
    
    //1.求字符串长度
    int length = str1.length(); //length=15  

    2.根据下标索引查询字符:charAt()

    //2.根据下标索引查询字符
    char ch = str1.charAt(5); //ch="i"

    3.截取字符串:substring()

    String str2= str1.substring(8); //str1="old str" ---> 从下标为8一直截取到最后
    
    //区间截取
    String str3 = str1.substring(8,12);//str3 ="old" --->从下标为8一直截取到下标为11([8,12)包左不包右

    4.单个字符查找、字符串查找:indexOf()

    //4.单个字符查找、字符串查找
    //若存在则返回字符或字符串从左起首次出现的下标位置,若没有则返回-1
    int a = str1.indexOf("t"); //a = 0
    int b = str1.indexOf("old str"); //b = 8
    int c = str1.indexOf("new str"); //c = -1

    5.字符串连接,效果等价于“+”:concat()

    //5.字符串连接,效果等价于“+”
     String str4 = "hello-".concat(str1).concat("-hello"); //hello-this is old str-hello

    6.字符串切分:split()

    //6.字符串切分
    String[] strings = str1.split(" ");// strings: {"this","is","old","str"}

    7.字符串比较:compareTo()和equals()

    //7.字符串比较
    //compareTo(String xxx):比较字符串字典顺序大小,比参数大返回正整数,反之返回负整数,相等返回0
    //compareToIgnore(String xxx):比较字符串字典顺序大小,忽略大小
    int  i=str1.compareTo("abc"); //i=19
    
    //equals():比较字符串内容值,相等返回true,反之返回false
    //equalsIgnoreCase():比较字符串内容值,忽略大小
    boolean flag = str1.equals(str2);//str1与str内容不一致,返回false

    8.字符串替换:replace()

    str1.replace("old","new");// this is new str
    //replaceAll():替换所有
    //replaceFirst():替换第一个出现的该字符

    9.字符串与基本类型转换

    //字符串转基本类型
    Integer.parseInt("123"); //输出:123
    Float.parseFloat("123"); //输出:123.0
    Double.parseDouble("123");//输出:123.0
    
    //基本类型转字符串
    String.valueOf(123); // "123"
    String.valueOf(123.0); // "123.0"
    String.valueOf(true); // "true"

     10.字符串转List

    //10.字符串转List集合
    String[] strings1 = str1.split(" ");
    List list = Arrays.asList(strings1);  // list: [this, is, old, str]
  • 相关阅读:
    /etc/sysctl.conf 控制内核相关配置文件
    python 并发编程 非阻塞IO模型
    python 并发编程 多路复用IO模型
    python 并发编程 异步IO模型
    python 并发编程 阻塞IO模型
    python 并发编程 基于gevent模块 协程池 实现并发的套接字通信
    python 并发编程 基于gevent模块实现并发的套接字通信
    python 并发编程 io模型 目录
    python 并发编程 socket 服务端 客户端 阻塞io行为
    python 并发编程 IO模型介绍
  • 原文地址:https://www.cnblogs.com/omgliyq/p/13945245.html
Copyright © 2020-2023  润新知