社区
Java SE
帖子详情
如何替换指定的字符串?
mychao2002
2002-11-13 02:05:29
比如我想把“abcderab"这个字符串中的‘ab’换成‘mn'怎么做?
谢谢!!
...全文
26
4
打赏
收藏
如何替换指定的字符串?
比如我想把“abcderab"这个字符串中的‘ab’换成‘mn'怎么做? 谢谢!!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
hrlixiang
2002-11-13
打赏
举报
回复
public static String Replace(String p_strSource, String p_strFrom, String p_strTo){
String p_strDest = "";
//get the length of the string
int l_intFromLen = p_strFrom.length();
//the position in the string
int l_intPos;
while((l_intPos=p_strSource.indexOf(p_strFrom))!=-1){
p_strDest = p_strDest + p_strSource.substring(0,l_intPos);
p_strDest = p_strDest + p_strTo;
p_strSource = p_strSource.substring(l_intPos+l_intFromLen);
}
p_strDest = p_strDest + p_strSource;
return p_strDest;
}
hotenM
2002-11-13
打赏
举报
回复
大哥,注释写的很清楚了
而且现在jdk1。4已经有可以用的函数了
public String repalceAll(String regex,String replacement)
mychao2002
2002-11-13
打赏
举报
回复
能具体解释一下吗 , 我不太明白……谢谢!!
hotenM
2002-11-13
打赏
举报
回复
jdk1.4里有一个方法
replaceAll
去看看吧
下面是我写的
/**
* 字符串替换函数
* @param sAll String 原来的字符串
* @param older String 要替换掉的字符串
* @param newer String 新的字符串
* @return String 替换后的结果
*/
public synchronized static String strReplace(String sAll,String sOld, String sNew){
int iT=0;
String sF = null,sH= null;
//如果新串中包括旧串,不让替多只让替少
if(sNew.indexOf(sOld)!= -1)
return sAll;
if(sAll == null || sOld==null ||sNew==null)
return sAll;
iT = sAll.indexOf(sOld);
int i = 0;
while(iT != -1){
sF = sAll.substring(0,iT);
sH= sAll.substring(iT+sOld.length());
sAll = sF+sNew+sH;
iT = sAll.indexOf(sOld);
}
return sAll;
}
php
替换
某个字符,php如何将
指定
字符串
替换
?
php将
指定
字符串
替换
的方法:1、【strtr】为转换
指定
字符,代码为【string strtr( string $str ,$replace_pairs )】;2、【str_replace()】函数以其他字符
替换
字符串
中的一些字符。php将
指定
字符串
替换
的方法:在PHP中,有两个函数可以实现
字符串
替换
,strtr()和str_repalce()函数。一、首先我们简单了解下strtr()函数的定义...
C 用新
字符串
替换
某
字符串
里的
指定
字符
/** *在有效的
字符串
内,用新
字符串
替换
指定
的
字符串
,仅执行一次
替换
操作 *str: 待
替换
的
字符串
源 *oldstr: 被
替换
的
字符串
*newstr: 用于
替换
的新
字符串
*result: 参数输出执行
替换
后的结果(可用该函数返回值,也可用result参数接收
替换
后的结果) **/ char *strrpc(char *str,char *oldstr,char *newstr,cha...
php将
指定
字符串
替换
,PHP如何
替换
指定
字符?
PHP如何
替换
指定
字符?在PHP中可以使用“str_replace()”函数
替换
指定
字符,该函数的作用是将子
字符串
替换
,其用法为“str_replace($search,$replace,$subject) ”,返回值为
替换
后的
字符串
。str_replace() 说明str_replace ( mixed $search , mixed $replace , mixed $subject [, i...
js
替换
html中的
字符串
,js怎么
替换
字符串
?
在js中,可以使用str.replace()方法来
替换
字符串
。replace()方法用于在
字符串
中用一些字符
替换
另一些字符,或
替换
一个与正则表达式匹配的子串;然后返回一个新的
字符串
。replace() 方法用于在
字符串
中用一些字符
替换
另一些字符,或
替换
一个与正则表达式匹配的子串。语法:stringObject.replace(regexp/substr,replacement)返回值一个新的
字符串
...
js将html
替换
字符串
,js
替换
字符串
指定
内容 javascript
替换
指定
位置的字符
js的replace如何全部
替换
目标
字符串
replace函数一次只能
替换
一个目标
字符串
,可以配合while循环
替换
所有
字符串
。在script标签中定义一个
字符串
,
字符串
内容为“一个中国北方的中国人在做中国结”,然后将内容输出在网页中:可以用replace函数去除
指定
字符串
。两种方式可以实现 1:使用replace函数
替换
var str="hello world。"; str=str.replac...
Java SE
62,615
社区成员
307,307
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章