java.lang.string
2016-02-02 13:59
381 查看
String结构
这个类结构很简单。。/** The value is used for character storage. */ private final char value[]; /** The offset is the first index of the storage that is used. */ private final int offset; /** The count is the number of characters in the String. */ private final int count; |
注意到String类是final的,不可以被继承,而且private final char value[];,只能赋值一次,赋值后就不能变了,只有从新生成一个String对象。
public String concat(String str)
public String concat(String str) { int otherLen = str.length(); if (otherLen == 0) { return this; } char buf[] = new char[count + otherLen]; getChars(0, count, buf, 0); str.getChars(0, otherLen, buf, count); return new String(0, count + otherLen, buf); } |
public int indexOf(String str, int fromIndex)
public int indexOf(String str, int fromIndex) { return indexOf(value, offset, count, str.value, str.offset, str.count, fromIndex); } //source源字符,sourceOffset源偏移,sourceCount源长度 //target查找的字符 ... static int indexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex) { //如果fromIndex比源字符还长(从0算起),并且查找的字符长度为0,那就返回源字符的长度,否则返回-1 if (fromIndex >= sourceCount) { return (targetCount == 0 ? sourceCount : -1); } if (fromIndex < 0) { fromIndex = 0; } //如果fromIndex比源字符短,查找的字符长度为0,直接返回fromIndex if (targetCount == 0) { return fromIndex; } //先取出第一个字符 char first = target[targetOffset]; int max = sourceOffset + (sourceCount - targetCount); //循环每一个字符 for (int i = sourceOffset + fromIndex; i <= max; i++) { /* 直到找到第一个字符 */ if (source[i] != first) { while (++i <= max && source[i] != first); } /* 找到第一个字符后,比较剩下的字符 */ if (i <= max) { int j = i + 1; int end = j + targetCount - 1; for (int k = targetOffset + 1; j < end && source[j] == target[k]; j++, k++); if (j == end) { /* 如果j能到end,那就说明找到整个字符串啦,返回偏移 */ return i - sourceOffset; } } } return -1; } |
public boolean equals(Object anObject)
public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = count; if (n == anotherString.count) { char v1[] = value; char v2[] = anotherString.value; int i = offset; int j = anotherString.offset; while (n-- != 0) { if (v1[i++] != v2[j++]) return false; } return true; } } return false; } |
replace与replaceAll
public String replace(CharSequence target, CharSequence replacement) { return Pattern.compile(target.toString(), Pattern.LITERAL).matcher( this).replaceAll(Matcher.quoteReplacement(replacement.toString())); } public String replaceAll(String regex, String replacement) { return Pattern.compile(regex).matcher(this).replaceAll(replacement); } |
replace只支持普通字符的替换哦,Pattern.LITERAL是指启用模式的字面值解析。Matcher.quoteReplacement(str)返回指定 String 的字面值替换 String。这个方法也就是替换\\为\\\\(四个反斜杠),$换为\\$。这样,它就不能用正则表达式了,但是看到依然是replaceAll,替换所有。
replaceAll很简单的一个正则表达式使用~~
要注意的是源字符串替换后内容并没有发生变化。
相关文章推荐
- Spring事务管理只对出现运行期异常进行回滚
- 0007-eclipse+OpenExplore
- Java class文件分析工具 -- Classpy
- java 常见异常总结
- JAVA_JCF(Java Collection Framework)学习笔记(二)
- JAVA设计模式初探之适配器模式
- Spring---搭建开发环境和第一个简单实例
- Spring概述
- JavaBean个人总结
- 聊聊高并发(二十五)解析java.util.concurrent各个组件(七) 理解Semaphore
- 设计模式之设计原则
- Java学习笔记(二)UML基础
- 深入理解Java的接口和抽象类
- Spring Hello World 实例
- Java设计模式之单例模式
- Java设计模式(十四)----模板方法模式
- java对象存储管理
- 整合Struts2+Spring2.5+Hibernate3.5
- java程序由于编码问题在cmd中运行失败
- spring hibernate整合时SessionFactory为空