Split string in classic asp

為{幸葍}努か 提交于 2019-12-11 09:04:41

问题


I am working in classic ASP and have a string as below :

A
B
C

Now I want to split the string, I have tried vbCrLf, vbNewline , vblf and
but none of them work.

Please suggest me an alternative to split the string. I am in a bad fix.


回答1:


Are you sure, you have newlines in the string?

First you can output all character codes to find out, by which character to split:

dim i, c

for i = 1 to len(my_string)
    c = mid(my_string, i, 1)
    Response.Write "CHAR: " & ASC(c) & " = " & c
next

Then you have 2 options:

  1. If you can split by one character (e.g. char num 10), you can use:

    a_result = split(my_string, CHR(10))
    
  2. You can grab values out of your string by using regular expression matching. This is much overhead, but if all else fails, here is how you could do that:

    function findStrings(s_text, s_pattern)
            dim a_out, obj_regex, obj_matches
            dim obj_match, n_index
            set obj_regex = New RegExp
    
            obj_regex.IgnoreCase = true
            obj_regex.Global = true
            obj_regex.MultiLine = true
            obj_regex.Pattern = s_pattern
            set obj_matches = obj_regex.execute(s_text)
            if obj_matches.Count>0 then
                    redim a_out(obj_matches.Count-1)
                    n_index = 0
                    for each obj_match in obj_matches
                            a_out(n_index) = cvStr(obj_match.Value)
                            n_index = n_index + 1
                    next
            end if
            findStrings = a_out
            set obj_regex = Nothing
    end function
    
    a_result = findStrings(my_string, "\w+")
    

This assumes, that there is no whitespace in the strings you are looking for.




回答2:


This happens more often than you think, you need to remove the vbcr first then replace only vblf and forget about spliting on vbcrlf because it wont work for 100% of the user envrioments out there.

A
B
C

' assuming about is in the variable str

split(replace(str,vbcr,""),vblf)


来源:https://stackoverflow.com/questions/16058903/split-string-in-classic-asp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!