📂Java
🔖String
2023-01-16 16:48阅读: 120评论: 0推荐: 0

String 常用方法总结

String 常用方法

常用实例方法

  1. 求字符串某一位置字符
    public char charAt(int index)// 返回字符串中指定位置的字符;注意字符串中第一个字符索引是 0,最后一个是 length()-1。

    1 String str = new String("asdfzxc");
    2 char ch = str.charAt(4);//ch = z
  2. 提取子串
    用 String 类的 substring 方法可以提取字符串中的子串,该方法有两种常用参数:
    1)public String substring(int beginIndex)// 该方法从 beginIndex 位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。
    2)public String substring(int beginIndex, int endIndex)// 该方法从 beginIndex 位置起,从当前字符串中取出到 endIndex-1 位置的字符作为一个新的字符串返回。

    1 String str1 = new String("asdfzxc");
    2 String str2 = str1.substring(2);//str2 = "dfzxc"
    3 String str3 = str1.substring(2,5);//str3 = "dfz"
  3. 字符串连接
    public String concat(String str)// 将参数中的字符串 str 连接到当前字符串的后面,效果等价于 "+"。

    1 String str = "aa".concat("bb").concat("cc");
    2 相当于 String str = "aa"+"bb"+"cc";
  4. 字符串中单个字符查找
    1)public int indexOf(int ch/String str)// 用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
    2)public int indexOf(int ch/String str, int fromIndex)// 改方法与第一种类似,区别在于该方法从 fromIndex 位置向后查找。
    3)public int lastIndexOf(int ch/String str)// 该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
    4)public int lastIndexOf(int ch/String str, int fromIndex)// 该方法与第二种方法类似,区别于该方法从 fromIndex 位置向前查找。

    1 String str = "I am a good student";
    2 int a = str.indexOf('a');//a = 2
    3 int b = str.indexOf("good");//b = 7
    4 int c = str.indexOf("w",2);//c = -1
    5 int d = str.lastIndexOf("a");//d = 5
    6 int e = str.lastIndexOf("a",3);//e = 2
  5. 字符串中字符的大小写转换
    1)public String toLowerCase()// 返回将当前字符串中所有字符转换成小写后的新串
    2)public String toUpperCase()// 返回将当前字符串中所有字符转换成大写后的新串

    1 String str = new String("asDF");
    2 String str1 = str.toLowerCase();//str1 = "asdf"
    3 String str2 = str.toUpperCase();//str2 = "ASDF"
  6. 字符串中字符的替换
    1)public String replace(char oldChar, char newChar)// 用字符 newChar 替换当前字符串中所有的 oldChar 字符,并返回一个新的字符串。
    2)public String replaceFirst(String regex, String replacement)// 该方法用字符 replacement 的内容替换当前字符串中遇到的第一个和字符串 regex 相匹配的子串,应将新的字符串返回。
    3)public String replaceAll(String regex, String replacement)// 该方法用字符 replacement 的内容替换当前字符串中遇到的所有和字符串 regex 相匹配的子串,应将新的字符串返回。

    1 String str = "asdzxcasd";
    2 String str1 = str.replace('a','g');//str1 = "gsdzxcgsd"
    3 String str2 = str.replace("asd","fgh");//str2 = "fghzxcfgh"
    4 String str3 = str.replaceFirst("asd","fgh");//str3 = "fghzxcasd"
    5 String str4 = str.replaceAll("asd","fgh");//str4 = "fghzxcfgh"
  7. 其他类方法

    1)String trim()// 截去字符串两端的空格,但对于中间的空格不处理。

    1 String str = " a sd ";
    2 String str1 = str.trim();
    3 int a = str.length();//a = 6
    4 int b = str1.length();//b = 4

    2)boolean startsWith(String prefix)boolean endWith(String suffix)// 用来比较当前字符串的起始字符 或子字符串 prefix 和终止字符或子字符串 suffix 是否和当前字符串相同,重载方法中同时还可以指定比较的开 始位置 offset。

    1 String str = "asdfgh";
    2 boolean a = str.startsWith("as");//a = true
    3 boolean b = str.endWith("gh");//b = true

    3)regionMatches(boolean b, int firstStart, String other, int otherStart, int length)// 从当前字符串的 firstStart 位置开始比较,取长度为 length 的一个子字符串,other 字符串从 otherStart 位置开始,指定另外一个长度为 length 的字符串,两字符串比较,当 b 为 true 时字符串不区分大小写。

    4)contains(String str)// 判断参数 s 是否被包含在字符串中,并返回一个布尔类型的值。

    1 String str = "student";
    2 str.contains("stu");//true
    3 str.contains("ok");//false

    5)String[] split(String str)// 将 str 作为分隔符进行字符串分解,分解后的字字符串在字符串数组中返回。

    1 String str = "asd!qwe|zxc#";
    2 String[] str1 = str.split("!|#");//str1[0] = "asd";str1[1] = "qwe";str1[2] = "zxc";

本文作者:九落

本文链接:https://www.cnblogs.com/9fall/p/17055821.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   九落  阅读(120)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.