String常用方法

ゝ一纸荒年。 2022-05-14 15:22 273阅读 0赞
  1. length()方法,返回字符串的长度

    1. public int length() {
    2. return value.length;
    3. }
    4. 例:
    5. String s = "I am fxyh!";
    6. int length = s.length();//length=10
  2. charAt()方法,返回一个字符,传入的索引不能小于0和大于等于字符串的长度

    1. public char charAt(int index) {
    2. if ((index < 0) || (index >= value.length)) {
    3. throw new StringIndexOutOfBoundsException(index);
    4. }
    5. return value[index];
    6. }
    7. 例:
    8. String s = "I am fxyh!";
    9. char charAt = s.charAt(5);//charAt = 'f'
  3. subString()方法,提取字符串里的内容,返回一个新字符串。

    1. public String substring(int beginIndex) {...}
    2. public String substring(int beginIndex, int endIndex) {...}
    3. 例:
    4. String s = "I am fxyh!";
    5. System.out.println(s.substring(2, 8));//am fxy
    6. System.out.println(s.substring(2));//am fxyh!
  4. 比较字符串方法

    1. compareTo两个字符串之间进行比较(区分大小写)

      源码:解释再多,不如看下源码

      1. public int compareTo(String anotherString) {
      2. int len1 = value.length;//原字符串的长度
      3. int len2 = anotherString.value.length;//传入字符串的长度
      4. int lim = Math.min(len1, len2);//两个字符串之间最短的长度
      5. char v1[] = value;//元字符串的char数组
      6. char v2[] = anotherString.value;//传入字符串的char数组
      7. int k = 0;
      8. while (k < lim) {//若果两个字符串中有最小长度大于0,则进入循环
      9. char c1 = v1[k];
      10. char c2 = v2[k];
      11. if (c1 != c2) {//如果第几位的char不同,则返回原字符的对应位的char类型的-传入的字符串对应为的差值,
      12. return c1 - c2;
      13. }
      14. k++;
      15. //如果传入的字符串和原字符串是包含关系的时候,也是返回长度差
      16. //就如:String str2 = "I AM FXYH!";
      17. //String str4 = "I AM FXYH!AAA"; 这样是返回两个字符串的长度差
      18. }
      19. return len1 - len2;
      20. }
    2. compareToIgnore,不区分大小写进行字符串比较

      源码:

      1. public int compareToIgnoreCase(String str) {
      2. return CASE_INSENSITIVE_ORDER.compare(this, str);
      3. }
      4. //String类的常量
      5. public static final Comparator<String> CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator();
      6. //内部内
      7. private static class CaseInsensitiveComparator implements Comparator<String>, java.io.Serializable {
      8. // use serialVersionUID from JDK 1.2.2 for interoperability
      9. private static final long serialVersionUID = 8575799808933029326L;
      10. public int compare(String s1, String s2) {
      11. int n1 = s1.length();
      12. int n2 = s2.length();
      13. int min = Math.min(n1, n2);
      14. for (int i = 0; i < min; i++) {
      15. char c1 = s1.charAt(i);
      16. char c2 = s2.charAt(i);
      17. if (c1 != c2) {
      18. c1 = Character.toUpperCase(c1);
      19. c2 = Character.toUpperCase(c2);
      20. if (c1 != c2) {
      21. c1 = Character.toLowerCase(c1);
      22. c2 = Character.toLowerCase(c2);
      23. if (c1 != c2) {
      24. // No overflow because of numeric promotion
      25. return c1 - c2;
      26. }
      27. }
      28. }
      29. }
      30. return n1 - n2;
      31. }
      32. /** Replaces the de-serialized object. */
      33. private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
      34. }
    3. equals区分大小写进行比较

      1. public boolean equals(Object anObject) {...}
    4. equalsIgnoreCase不区分大小写进行比较

      1. public boolean equalsIgnoreCase(String anotherString) {...}
  5. 字符串连接将参数中的字符串str连接到当前字符串的后面,效果等价于”+”。

    1. String s = "I".concat("am").concat("fxyh");
    2. 效果相当于String s = "I"+"am"+"fxyh";
    3. 但是效率明显没有“+”高
  6. 单个字符查找

    1. public int indexOf(int ch) {

      1. return indexOf(ch, 0);

      }
      public int indexOf(int ch, int fromIndex) {…}

      //查找字符串里面的数字,查到返回位置,没查到返回负数,fromIndex表示从哪个位置开始找

    2. public int indexOf(String str) {
      1. return indexOf(str, 0);
      }
      public int indexOf(String str, int fromIndex) {…}
      //查找字符串里的字符串,查到返回位置,没查到也是返回负数,fromIndex表示从哪个位置开始找
    3. lastIndexOf和indexOf类似,只是从后面往前面找。
  7. 字符串大小写转换

    1. toUpperCase()//将字符串转成大写,然后返回新字符串
    2. toLowerCase()//将字符串转成小写,然后返回新字符串
  8. trim() //截去字符串两端的空格,然后返回新字符串
  9. boolean statWith(String prefix)判断字符串是不是以prefix字符串开头
  10. boolean endWith(String suffix)判断字符串是不是以suffix字符串结尾
  11. String[] split(String str)//将字符串以str进行拆分,分解成String[]。

发表评论

表情:
评论列表 (有 0 条评论,273人围观)

还没有评论,来说两句吧...

相关阅读