java.lang.String(JDK源码分析)

String结构

这个类结构很简单。。

[java]  view plain  copy
  1. /** The value is used for character storage. */    
  2.     private final char value[];    
  3.      
  4.     /** The offset is the first index of the storage that is used. */    
  5.     private final int offset;    
  6.      
  7.     /** The count is the number of characters in the String. */    
  8.     private final int count;    

用了一个char数组来存储字符,然后offset是偏移(这个我还搞不懂有啥用),count是String的长度。
注意到String类是final的,不可以被继承,而且private final char value[];,只能赋值一次,赋值后就不能变了,只有从新生成一个String对象。

public String concat(String str)

[java]  view plain  copy
  1.    public String concat(String str) {    
  2. int otherLen = str.length();    
  3. if (otherLen == 0) {    
  4.     return this;    
  5. }    
  6. char buf[] = new char[count + otherLen];    
  7. getChars(0, count, buf, 0);    
  8. str.getChars(0, otherLen, buf, count);    
  9. return new String(0, count + otherLen, buf);    
  10.    }    


这段代码是连接两个String的,先拿个char数组当容器,从this和str分别取出char放入buf内,注意getChars方法的第四个参数,这个是说buf从count个开始拷贝。也就是说把两个string的char放在了一个char数组内,再返回一个新的String对象。(因为之前的String已经不可以改变)

public int indexOf(String str, int fromIndex)

[java]  view plain  copy
  1. public int indexOf(String str, int fromIndex) {    
  2.        return indexOf(value, offset, count,    
  3.                       str.value, str.offset, str.count, fromIndex);    
  4.    }    
  5.     
  6.    //source源字符,sourceOffset源偏移,sourceCount源长度    
  7.    //target查找的字符 ...    
  8.    static int indexOf(char[] source, int sourceOffset, int sourceCount,    
  9.                       char[] target, int targetOffset, int targetCount,    
  10.                       int fromIndex) {    
  11.        //如果fromIndex比源字符还长(从0算起),并且查找的字符长度为0,那就返回源字符的长度,否则返回-1    
  12. if (fromIndex >= sourceCount) {    
  13.            return (targetCount == 0 ? sourceCount : -1);    
  14. }    
  15.     if (fromIndex < 0) {    
  16.         fromIndex = 0;    
  17.     }    
  18.        //如果fromIndex比源字符短,查找的字符长度为0,直接返回fromIndex    
  19. if (targetCount == 0) {    
  20.     return fromIndex;    
  21. }    
  22.     
  23.        //先取出第一个字符    
  24.        char first  = target[targetOffset];    
  25.        int max = sourceOffset + (sourceCount - targetCount);    
  26.     
  27.        //循环每一个字符    
  28.        for (int i = sourceOffset + fromIndex; i <= max; i++) {    
  29.            /* 直到找到第一个字符 */    
  30.            if (source[i] != first) {    
  31.                while (++i <= max && source[i] != first);    
  32.            }    
  33.     
  34.            /* 找到第一个字符后,比较剩下的字符 */    
  35.            if (i <= max) {    
  36.                int j = i + 1;    
  37.                int end = j + targetCount - 1;    
  38.                for (int k = targetOffset + 1; j < end && source[j] ==    
  39.                         target[k]; j++, k++);    
  40.     
  41.                if (j == end) {    
  42.                    /* 如果j能到end,那就说明找到整个字符串啦,返回偏移 */    
  43.                    return i - sourceOffset;    
  44.                }    
  45.            }    
  46.        }    
  47.        return -1;    
  48.    }    


indexOf只要看它的查找方法,先找到第一个,然后再匹配剩下的。

public boolean equals(Object anObject)

[java]  view plain  copy
  1. public boolean equals(Object anObject) {    
  2.     if (this == anObject) {    
  3.         return true;    
  4.     }    
  5.     if (anObject instanceof String) {    
  6.         String anotherString = (String)anObject;    
  7.         int n = count;    
  8.         if (n == anotherString.count) {    
  9.         char v1[] = value;    
  10.         char v2[] = anotherString.value;    
  11.         int i = offset;    
  12.         int j = anotherString.offset;    
  13.         while (n-- != 0) {    
  14.             if (v1[i++] != v2[j++])    
  15.             return false;    
  16.         }    
  17.         return true;    
  18.         }    
  19.     }    
  20.     return false;    
  21.     }    


比较char~~

replace与replaceAll

[java]  view plain  copy
  1. public String replace(CharSequence target, CharSequence replacement) {    
  2.        return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(    
  3.            this).replaceAll(Matcher.quoteReplacement(replacement.toString()));    
  4.    }    
  5.     
  6.    public String replaceAll(String regex, String replacement) {    
  7. return Pattern.compile(regex).matcher(this).replaceAll(replacement);    
  8.    }    


这两句真短啊。。用正则表达式来替换的。可以看出他们的区别。
replace只支持普通字符的替换哦,Pattern.LITERAL是指启用模式的字面值解析。Matcher.quoteReplacement(str)返回指定 String 的字面值替换 String。这个方法也就是替换\\为\\\\(四个反斜杠),$换为\\$。这样,它就不能用正则表达式了,但是看到依然是replaceAll,替换所有。
replaceAll很简单的一个正则表达式使用~~
要注意的是源字符串替换后内容并没有发生变化。
举例如下: 来源

[java]  view plain  copy
  1. String src = new String(“ab43a2c43d”);  
  2. System.out.println(src.replace(“3″,”f”));=>ab4f2c4fd.  
  3. System.out.println(src.replace(’3′,’f'));=>ab4f2c4fd.  
  4. System.out.println(src.replaceAll(“\\d”,”f”));=>abffafcffd.  
  5. System.out.println(src.replaceAll(“a”,”f”));=>fb43fc23d.  
  6. System.out.println(src.replaceFirst(“\\d,”f”));=>abf32c43d  
  7. System.out.println(src.replaceFirst(“4″,”h”));=>abh32c43d.  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值