+ -
当前位置:首页 → 问答吧 → 急哇:每隔N个字符自动加入指定的字符,如果内容中以有此指定字符就要以已有这个字符后来算。

急哇:每隔N个字符自动加入指定的字符,如果内容中以有此指定字符就要以已有这个字符后来算。

时间:2011-08-27

来源:互联网

提交来的内容:“ABC|DEFGHIJKLMNOPQRSTUVWXYZ”
最终要的内容:“ABC|DEFGH|IJKLM|NOPQR|STUVW|XYZ|”

每隔5个字符之后就在后面自动加入“|”这个我自定的分页符,如果提交来的内容中已经有“|”

字符那么就成从“|”之后开始5个字符后加入,应该就是这个意思。本人表达能力不是非常的好

。下面是我现在以经写好的每5个字符之后自动加入“|”,但如果提交的内容里面就有“|”的话

重新从“|”之后开始计算,这样子应该怎么搞?没思路不会了。。。

VB code

<%
'================================================
'函数名:FormatChar
'作 用:每隔N个字符自动加入指定的字符
'参 数:Str, Length, RepStr (原字符串, 相隔长度, 插入字符)
'返回值:处理过后的数据
'================================================
Function FormatChar(Str,Length,RepStr)
    Dim CharLen
    CharLen = Len(Str) / Length
    If CharLen <> Int(CharLen) Then
        CharLen = Int(CharLen) + 1
    End If
    Dim I, Content
    For I = 1 To CharLen    
        Content = Content & Mid(Str, (I-1)*Length+1, Length)&RepStr
    Next
    FormatChar = Content
End Function

Response.Write FormatChar("ABC|DEFGHIJKLMNOPQRSTUVWXYZ", 5, "|")
%>



高手帮忙哇,,谢谢了,真的非常需要这个。

作者: diyuxuemo   发布时间: 2011-08-27

<%
'================================================
'函数名:FormatChar
'作 用:每隔N个字符自动加入指定的字符
'参 数:Str, Length, RepStr (原字符串, 相隔长度, 插入字符)
'返回值:处理过后的数据
'================================================
Function FormatChar(Str,Length,RepStr)
  Dim I, CharLen,Content,startChar
CharLen = Len(Str)
if instr(Str,RepStr)>0 then
  startChar=instr(Str,RepStr)
else
startChar=1
end if

  For I = startChar To CharLen step 5
  Content = Content & Mid(Str, I, Length)&RepStr
  Next
  FormatChar = Content
End Function

Response.Write FormatChar("ABC|DEFGHIJKLMNOPQRSTUVWXYZ", 5, "|")
%>

作者: lzp4881   发布时间: 2011-08-27

如果还要考虑字符口串中有多个|的情况
<%
'================================================
'函数名:FormatChar
'作 用:每隔N个字符自动加入指定的字符
'参 数:Str, Length, RepStr (原字符串, 相隔长度, 插入字符)
'返回值:处理过后的数据
'================================================
Function FormatChar(Str,Length,RepStr)
  Dim I, CharLen,Content,startChar
CharLen = Len(Str)
if instrRev(Str,RepStr)>0 then
  startChar=instrRev(Str,RepStr)
Content=Mid(Str,1,startChar)
else
startChar=1
Content=""
end if

  For I = startChar+1 To CharLen step 5
  Content = Content & Mid(Str, I, Length)&RepStr
  Next
  FormatChar = Content
End Function

Response.Write FormatChar("ABC|DEFG|HIJKLMNOPQRSTUVWXYZ", 5, "|")
%>

作者: lzp4881   发布时间: 2011-08-27

Response.Write FormatChar("AB|C111111fasd1111|DEFG|HI|JKLMNOPQRSTUVWXYZ", 5, "|")

作者: afu45   发布时间: 2011-08-27

HTML code

<%
'================================================
'函数名:FormatChar
'作 用:每隔N个字符自动加入指定的字符
'参 数:Str, Length, RepStr (原字符串, 相隔长度, 插入字符)
'返回值:处理过后的数据
'================================================
Function FormatChar(Str,Length,RepStr)
    Dim I, CharLen,Content,startChar
    newstr=split(Str,RepStr)
    Content=""
    For I = 0 To UBound(newstr)
        if Len(newstr(i))<>0 then
            if Len(newstr(i))<6 then
                Content = Content & newstr(i)&RepStr
            else
                tmpLen=Len(newstr(i))
                For j = 1 To tmpLen step 5
                Content = Content & Mid(newstr(i), j, 5)&RepStr
                Next
            end if
        end if
    Next
    FormatChar = Content
End Function
Response.Write FormatChar("ABCDEFGHIJKLMNOPQ|1234567890|12|123|1234|12345|123456|1234567|", 5, "|")
%>



ABCDEFGHIJKLMNOPQ|1234567890|12|123|1234|12345|123456|1234567|
输出:
ABCDE|FGHIJ|KLMNO|PQ|12345|67890|12|123|1234|12345|12345|6|12345|67|

作者: afu45   发布时间: 2011-08-27