请教大牛们一个问题

aloveu 2012-02-10 04:18:56
字符串的 public String replace(char oldChar,
char newChar)

该方法具体是怎么实现的? 哪里可以查到? API里面只讲了用法,没给出实现啊
...全文
114 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttwdr 2012-02-11
  • 打赏
  • 举报
回复
JDK 源代码:
/**
* Returns a new string resulting from replacing all occurrences of
* <code>oldChar</code> in this string with <code>newChar</code>.
* <p>
* If the character <code>oldChar</code> does not occur in the
* character sequence represented by this <code>String</code> object,
* then a reference to this <code>String</code> object is returned.
* Otherwise, a new <code>String</code> object is created that
* represents a character sequence identical to the character sequence
* represented by this <code>String</code> object, except that every
* occurrence of <code>oldChar</code> is replaced by an occurrence
* of <code>newChar</code>.
* <p>
* Examples:
* <blockquote><pre>
* "mesquite in your cellar".replace('e', 'o')
* returns "mosquito in your collar"
* "the war of baronets".replace('r', 'y')
* returns "the way of bayonets"
* "sparring with a purple porpoise".replace('p', 't')
* returns "starring with a turtle tortoise"
* "JonL".replace('q', 'x') returns "JonL" (no change)
* </pre></blockquote>
*
* @param oldChar the old character.
* @param newChar the new character.
* @return a string derived from this string by replacing every
* occurrence of <code>oldChar</code> with <code>newChar</code>.
*/
public String replace(char oldChar, char newChar) {
if (oldChar != newChar) {
int len = count;
int i = -1;
char[] val = value; /* avoid getfield opcode */
int off = offset; /* avoid getfield opcode */

while (++i < len) {
if (val[off + i] == oldChar) {
break;
}
}
if (i < len) {
char buf[] = new char[len];
for (int j = 0 ; j < i ; j++) {
buf[j] = val[off+j];
}
while (i < len) {
char c = val[off + i];
buf[i] = (c == oldChar) ? newChar : c;
i++;
}
return new String(0, len, buf);
}
}
return this;
}
CaiKanXP 2012-02-10
  • 打赏
  • 举报
回复
Java? 这里是Javascript板块。

想看实现的话,去JDK里面找吧,里面有源码。
aloveu 2012-02-10
  • 打赏
  • 举报
回复
有chm格式的文档发出来共享哈 下周2结贴 希望大家踊跃发言啊
aloveu 2012-02-10
  • 打赏
  • 举报
回复
只有html的吗? 有没有chm格式的?
gnik2018 2012-02-10
  • 打赏
  • 举报
回复
想知道实现的话去下个js直接收索就知道了
gnik2018 2012-02-10
  • 打赏
  • 举报
回复
http://www.cnblogs.com/mahaisong/archive/2011/02/28/1967208.html
三石-gary 2012-02-10
  • 打赏
  • 举报
回复
这个要去找实现它的源码。。。在你引用这个方法的时候有没有引用什么东西啊。。在里面应该能找到。。不然的话就能找了

87,989

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧