zoukankan      html  css  js  c++  java
  • 常用String类方法-Java

    高频###

    • int length():返回字符串的长度:return value.length

    • char charAt(int index):返回某索引处的字符:return value[index]

    • boolean isEmpty():判断是否是空字符串:return value.length == 0

    • String toLowerCase():使用默认语言环境,将String中的所有字符转换转换成小写

    • String toUpperCase():使用默认语言,将String中的所有字符转换成大写

    • String trim():返回字符串的副本,忽略前导空白和尾部空白

    • boolean equals(Object obj):比较字符串的内容是否相同

    • boolean equalsIgnoreCase(String anotherString):与equals类似,忽略大小写

    • String concat(String str):将指定字符串连接到此字符串的结尾,等价于用“+”

    • int compareTo(String anotherString):比较两个字符串的大小

    • String substring(int beginIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后的一个子字符串

    • String substring(int beginIndex,int endIndex):返回一个新字符串,它是此字符串从beginIndex开始截取到endIndex(不包含)的一个子字符串

    检索###

    • boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束

    • boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始

    • boolean startsWith(String Prefix,int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始

    • boolean contains(CharSequence s):当且仅当此字符串包含指定的char值序列时,返回true

    • int indexOf(String str):返回指定子字符串在此字符串中第一次出现的索引

    • int indexOf(String str,int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始

    • int lastIndexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引

    • int lastIndexOf(String str,int fromIndex):返回指定子字符串中最后出现在此字符串中的索引,从指定的索引开始反向搜索

    注:indexOf和lastIndexOf方法如未找到指定值都返回-1

    替换###

    • String replace(char oldChar,char newChar):用newChar替换此字符串中所有的oldChar,返回一个新的字符串
    • String repalce(CharSequence target,CharSequence replacement):使用指定的字面值‘替换序列’替换此字符串所有匹配字面值‘目标序列’的子字符串
    • String replaceAll(String regex,String replacement):使用给定的replacement替换此字符串所有匹配给定的正则表达式的子字符串
    • String replaceFilrst(String regex,String replacement):使用给定的replacement替换此字符串匹配给定的的正则表达式的第一个子字符串

    匹配###

    • boolean matches(String regex):告知此字符串是否匹配给定的正则表达式

    切片(拆分)###

    • String[] split(String regex):根据给定正则表达式的匹配拆分此字符串
    • String[] split(String regex,int limit):根据匹配给定的正则表达水来拆分此字符串,最多不超过limit个,如果超过了,剩下的全部都放到最后一个元素中
  • 相关阅读:
    【题解】Luogu CF817F MEX Queries
    【题解】Luogu P4396 [AHOI2013]作业
    【题解】Luogu P4198 楼房重建
    【题解】Luogu P1471 方差
    【题解】Luogu P4069 [SDOI2016]游戏
    【题解】Luogu P4097 [HEOI2013]Segment
    李超线段树略解
    【题解】JSOIWC2019 Round 5
    【题解】Luogu P2763 试题库问题
    【题解】JSOIWC2019 Round4
  • 原文地址:https://www.cnblogs.com/csyh/p/12269402.html
Copyright © 2011-2022 走看看