请先登录 | 注册
MYCSG.CN
[ASP语法] Replace() 替换变量或者字符串中的某字符 - 文章首页返回版区
■标题:[ASP语法] Replace() 替换变量或者字符串中的某字符
■作者:IPSC [2005/12/12 8:59:44]

Replace()
功能(英): Returns a string in which a specified sub-string has been replaced with another substring a specified number of times.
功能(中):替换变量或者字符串(strToBeSearched)中特定的字符(strSearchFor)为指定的字符(strReplaceWith ),甚至可以设置从第几个(start )出现的开始替换,共替换多少个(count )。
语法: Replace(strToBeSearched, strSearchFor, strReplaceWith , start , count , compare)
条件(英): strToBeSearched is a string expression containing a sub-string to be replaced; strSearchFor is the string expression to search for within strToBeSearched; strReplaceWith is the string expression to replace sub-string strSearchFor; start (optional) is the numeric character position to begin search; count (optional) is a value indicating the comparision constant.(看不懂的看例子即可理解)
例子: <%
  strTest = "This is an apple!"
  response.write Replace(strTest, "apple", "orange")
  %>
运行结果: This is an orange!

IPSC附言:在BBSXP中用到的和例子中的一样,我的插件中也很常用此语法,至于更高级的功能,设置从第几个出现的开始替换,共替换多少个这样的功能,BBSXP中很少用。

 

[此帖子已被 IPSC 在 2005-12-12 9:01:14 编辑过]

回复/版区/上篇/下篇/发贴/仅文字/HTML

【倒序/最近21条回复】
■作者:IPSC [2006/1/8 12:11:51]
这个以后可能用其它技巧还是可以实现,到时再回头来解决这个问题

■作者:cnpoet [2006/1/6 14:42:16]
不知道如果要在.NET中实现   从第几个出现的开始替换   应该怎么做.

■作者:cnpoet [2006/1/6 14:35:44]

比较了一下,ASP中 设置从第几个出现的开始替换,但在.NET中,这个 start 却不是指第几个出现,而是指 指定字符串的第几个字符!

如:

<%
Dim test="01-My love is Nongnong-02-My love is Nongnong-03-My love is Nongnong-04-My love is Nongnong-05-My love is Nongnong-06-My love is Nongnong"
response.write(Replace(test,"Nongnong","Pet",6,3))
%>

运行结果:

love is Pet-02-My love is Pet-03-My love is Pet-04-My love is Nongnong-05-My love is Nongnong-06-My love is Nongnong

<%Dim test="01-My love is Nongnong-02-My love is Nongnong-03-My love is Nongnong-04-My love is Nongnong-05-My love is Nongnong-06-My love is Nongnong"response.write(Replace(test,"Nongnong","Pet",6,3))%>

(查看完整版网页)



短讯|首页|登录|算法|电脑版 .
DK MiniBBS Plus v2.0
mm0759.com
140 毫秒 .