• String类


    • 字符串查找

    5-3 String类的字符串查找方法

    方法定义

    功能说明

    char charAt(int index)

    返回在index位置的字符,须满足0indexlength()-1,否则抛出IndexOutOfBoundsException异常

    int indexOf(int ch)

    返回字符ch在字符串中第一次出现的位置,若未出现,则返回-1

    int indexOf(int ch, int fromIndex)

    返回从fromIndex开始字符ch在字符串中第一次出现的位置,若未出现,则返回-1

    int lastIndexOf(int ch)

    返回字符ch在字符串中从后往前第一次出现的位置,若未出现,则返回-1

    int lastIndexOf(int ch, int fromIndex)

    返回字符ch在字符串中从fromIndex往前第一次出现的位置,若未出现,则返回-1

    int indexOf(String str)

    若能在字符串找到某个子串与str完全相同,则返回str的首字符在字符串中第一次出现的位置,否则返回-1

    int indexOf(String str, int fromIndex)

    在字符串中从fromIndex开始若能找到某个子串与str完全相同,则返回str的首字符在字符串中第一次出现的位置,否则返回-1

    int lastIndexOf(String str)

    若能在字符串中从后往前找到某个子串与str完全相同,则返回str的首字符在字符串中第一次出现的位置,否则返回-1

    int lastIndexOf(String str, int fromIndex)

    若能在字符串中从fromIndex开始往前找到某个子串与str完全相同,则返回str的首字符在字符串中第一次出现的位置,否则返回-1

    //将原有字符串中所有的oldChar字符全部替换成newChar

    public String replace(char oldChar, char newChar)

    public String toLowerCase()//转成小写

    public String toUpperCase()//转成大写

    public String trim()//去掉前后的空格

    String substring(int beginIndex) 获取从beginIndex位置开始到结束的子字符串

    String substring(int beginIndex, int endIndex) 获取从beginIndex位置开始到endIndex位置的子字符串

    “= =”用于判断基本数据类型的值是否一样,以及判断引用数据类型的空间地址是否一样;

    “equals”只用于判断引用数据类型中的内容是否一样

    String 对象创建后则不能被修改,是不可变的,所谓的修改其实是创建了新的对象,所指向的内存空间不同。

    StringBuffer 是线程安全的,而 StringBuilder 则没有实现线程安全功能,所以性能略高。因此一般情况下,如果需要创建一个内容可变的字符串对象,应优先考虑使用 StringBuilder 类。

  • 相关阅读:
    toad 快捷键大全
    validateRequest 相关的作用
    为何有着良好设计的系统代码反而不容易看懂?
    致命错误: zlib.h:没有那个文件或目录
    全局变量相互依赖和初始化顺序的解决办法
    解决“possibly undefined macro: AC_PROG_LIBTOOL”
    Rsync完全配置
    undefined reference to `clock_gettime'
    解决“configure: line 2747: g: command not found”
    openssl编程轻松入门(含完整示例)
  • 原文地址:https://www.cnblogs.com/rongweijun/p/5440624.html
Copyright © 2020-2023  润新知