str1=Replace("陈明(1)张史(1)周细林(1)","(1)","(2)",8,1)的结果应该是 "陈明(1)张史(2)周细林(1)",但是结果却是"(2)周细林(1)",真是有点不明白?

解决方案 »

  1.   

    摘自MSDN:Replace 函数  语言参考 
    起始页 | 版本 2 
     请参阅 
    --------------------------------------------------------------------------------描述
    返回字符串,其中指定数目的某子字符串被替换为另一个子字符串。
    语法
    Replace(expression, find, replacewith[, compare[, count[, start]]])
    Replace 函数的语法有以下参数:参数 描述 
    expression 必选。 字符串表达式 包含要替代的子字符串。  
    find 必选。被搜索的子字符串。 
    replacewith 必选。用于替换的子字符串。 
    start 可选。expression 中开始搜索子字符串的位置。如果省略,默认值为 1。在和count 关联时必须用  
    count 可选。执行子字符串替换的数目。如果省略,默认值为 -1,表示进行所有可能的替换。在和 start 关联时必须用。 
    compare 可选。指示在计算子字符串时使用的比较类型的数值。有关数值,请参阅“设置”部分。如果省略,缺省值为 0 ,这意味着必须进行二进制比较。 
    设置
    compare 参数可以有以下值:
    常数 值 描述 
    vbBinaryCompare 0 执行二进制比较。 
    vbTextCompare 1 执行文本比较。 返回值
    Replace 返回以下值:
    如果 Replace 返回 
    expression 为零长度 零长度字符串 ("")。 
    expression 为 Null 错误。 
    find 为零长度 expression 的副本。 
    replacewith 为零长度 expression 的副本,其中删除了所有由 find 参数指定的内容。 
    start > Len(expression) 零长度字符串。 
    count 为 0 expression 的副本。 
    说明
    Replace 函数的返回值是经过替换(从由 start 指定的位置开始到 expression 字符串的结尾)后的字符串,而不是原始字符串从开始至结尾的副本。
    下面的示例利用 Replace 函数返回字符串: Dim MyString'二进制比较从字符串左端开始。返回 "XXYXXPXXY"。
    MyString = Replace("XXpXXPXXp", "p", "Y") '文本比较从第三个字符开始。返回 "YXXYXXY"。
    MyString = Replace("XXpXXPXXp", "p", "Y", 3, -1, 1) 
      

  2.   

    Replace函数的返回值是一个字符串,但是,其中从start所指定的位置开始,到expression字符串的结尾处的一段子字符串已经发生过替换动作。并不是原字符串从头到尾的一个复制
    上面的说明已经指出这个问题,要是返回前面的,用left()函数截取一下前面的7个字符吧.
      

  3.   

    楼主对replace函数理解错误
    如果你想将字符串:"陈明(1)张史(1)周细林(1)",修改为:"陈明(1)张史(2)周细林(1)",最高方法是用mid语句:dim s as string
    s="陈明(1)张史(1)周细林(1)"
    mid(s,9,1)="2"
    msgbox s
      

  4.   

    str1 = Left("陈明(1)张史(1)周细林(1)",7) & _
           Replace("陈明(1)张史(1)周细林(1)","(1)","(2)", 8,1)
      

  5.   

    本函数估计描述有误:“返回一个字符串,该字符串中指定的子字符串已被替换成另一子字符串,并且替换发生的次数也是指定的
    start 可选的。在表达式中子字符串搜索的开始位置。如果忽略,假定从1开始。 ”事实上要这么描述:“返回一个字符串,该字符串中指定的子字符串已被替换成另一子字符串,并且替换发生的次数也是指定的
    start 可选的。在表达式中子字符串搜索的开始位置。如果忽略,假定从1开始。如果大于1,那么字符串中这个数前面的字符将被剔除。 ”
      

  6.   

    事实上,我认为应该是函数bug。既然是从指定位置替换指定次数的自付串,那么前面的字符不应该剔除而要保留,效果要达到start=1的效果才对。
      

  7.   

    不存在bug,1楼帖的MSND中介绍replace函数相关内容中的说明部分,已经说明的清楚了...在处理字符串中这种功能是常用的...
      

  8.   

    怪事,英文MSDN和中文的MSDN参数顺序怎么不同?Visual Basic for Applications ReferenceReplace Function
          DescriptionReturns a string in which a specified substring has been replaced with another substring a specified number of times.SyntaxReplace(expression, find, replace[, start[, count[, compare]]])
    The Replace function syntax has these named arguments:Part Description 
    expression Required. String expression containing substring to replace. 
    find Required. Substring being searched for. 
    replace Required. Replacement substring. 
    start Optional. Position within expression where substring search is to begin. If omitted, 1 is assumed. 
    count Optional. Number of substring substitutions to perform. If omitted, the default value is –1, which means make all possible substitutions. 
    compare Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. See Settings section for values. 
    SettingsThe compare argument can have the following values:Constant Value Description 
    vbUseCompareOption –1 Performs a comparison using the setting of the Option Compare statement. 
    vbBinaryCompare 0 Performs a binary comparison. 
    vbTextCompare 1 Performs a textual comparison. 
    vbDatabaseCompare 2 Microsoft Access only. Performs a comparison based on information in your database. 
    Return ValuesReplace returns the following values:If Replace returns 
    expression is zero-length Zero-length string ("") 
    expression is Null An error. 
    find is zero-length Copy of expression. 
    replace is zero-length Copy of expression with all occurences of find removed. 
    start > Len(expression) Zero-length string. 
    count is 0 Copy of expression. 
    ResThe return value of the Replace function is a string, with substitutions made, that begins at the position specified by start and and concludes at the end of the expression string. It is not a copy of the original string from start to finish.--------------------------------------------------------------------------------
    Send feedback to MSDN.Look here for MSDN Online resources. 
      

  9.   

    VBA 和 VB 是俩兄弟,生活习惯不同很正常。
      

  10.   


    replace常用,可是掌握的知识没有这么细.