VBScript - 用 2 个目标字符拆分字符串,将它们转换为复合数组,然后从数组中删除不必要的内容

发布于 2025-01-11 00:16:39 字数 1236 浏览 0 评论 0原文

我有一个问题,可能容易,也可能不容易。

我将进一步解释:我有一个像这样的字符串:

str = "TR~Maintenance~fas plus-maintenicon|GR~Supplies~fas minus-suppicon|JK~Affidavit~fas minus-affiicon"

现在,我的目标是将它们分别放入一个数组中,例如:

TR
Maintenance
fas plus-maintenicon
GR
Supplies
fas minus-suppicon
JK
Affidavit
fas minus-affiicon

然后重新整形和重新调整数组以仅保留以 fas 开头的字符串......

什么是最简单的方法吗?分裂?正则表达式?在Str?

我有以下代码:

Function funcSeparaLista(objLst)
    arrVLst = split(objLst, "~")
    
    for each x in arrVLst
        VLst = VLst&x&"<br/>"
    next
        
    funcSeparaLista = split(VLst, "|")
end function

t = funcSeparaLista(str)

但是使用以下方法检索数据:

response.write(t(0))
response.write(t(1))
response.write(t(2))
response.write(t(3))

我得到了不需要的结果:

TR
Maintenance
fas plus-mainteniconGR
Supplies
fas minus-suppiconJK
Affidavit
fas minus-affiicon

最重要的一件事是:我希望我的 FUNCTION 仅处理将分离的字符串放入数组中并在代码的另一部分中处理它。

在数组按照我想要的方式保存所有内容之后,我只希望数组保留这些数据:

fas plus-maintenicon
fas minus-suppicon
fas minus-affiicon

我做错了什么?请记住。这是 vbscript,不是 VB.Net。 提前致谢,我认为这可能非常简单,但在重做和检查代码几个小时后我并没有完全尝试完成它。

I have a question that could be easy or not.

I will explain further: I have a string like this:

str = "TR~Maintenance~fas plus-maintenicon|GR~Supplies~fas minus-suppicon|JK~Affidavit~fas minus-affiicon"

Now, my objective is to put those in an array separately like:

TR
Maintenance
fas plus-maintenicon
GR
Supplies
fas minus-suppicon
JK
Affidavit
fas minus-affiicon

and then reshape and redim the array to keep only, let's say, the strings starting with fas....

What is the easiest method? Spliting? regEx? inStr?

I have the following code:

Function funcSeparaLista(objLst)
    arrVLst = split(objLst, "~")
    
    for each x in arrVLst
        VLst = VLst&x&"<br/>"
    next
        
    funcSeparaLista = split(VLst, "|")
end function

t = funcSeparaLista(str)

But retrieving the data using:

response.write(t(0))
response.write(t(1))
response.write(t(2))
response.write(t(3))

I get undesired results:

TR
Maintenance
fas plus-mainteniconGR
Supplies
fas minus-suppiconJK
Affidavit
fas minus-affiicon

One thing most important: I want my FUNCTION only to deal with putting the detached string in arrays and dealing with it after in another portion of code.

After the array hold everything like I want it, I want only the array to keep this data:

fas plus-maintenicon
fas minus-suppicon
fas minus-affiicon

What I am doing wrong? Remember please. This is vbscript, not VB.Net.
Thanks in advance I think this could be very easy but I am not quite trying to accomplish it after some hours redoing and checking code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2025-01-18 00:16:39

这是一个序列化字符串,其中记录分隔符为 |,字段分隔符为 ~。您可以使用双循环轻松构建二维数组。

Dim str: str = "TR~Maintenance~fas plus-maintenicon|GR~Supplies~fas minus-suppicon|JK~Affidavit~fas minus-affiicon"

Dim data: data = ConvertToArray(str, "|", "~")
WScript.Echo data(1, 2)

Function ConvertToArray(value, record_delim, field_delim)
    Dim data()
    If Len(value) < 1 Then ConvertToArray = Empty
    Dim records: records = Split(value, record_delim)
    If Not IsArray(records) Then ConvertToArray = Empty
    Dim rows: rows = UBound(records)
    Dim row, col
    For row = 0 To rows
        Dim fields: fields = Split(records(row), field_delim)
        If Not IsArray(fields) Then ConvertToArray = Empty
        Dim cols: cols = UBound(fields)
        ReDim Preserve data(cols, row)
        For col = 0 To cols
            data(col, row) = fields(col)
        Next
    Next
    ConvertToArray = data
End Function

输出:

Affidavit

使用此函数,您可以通过循环并仅返回每行的第三列来获得所需的输出。

Dim data: data = ConvertToArray(str, "|", "~")
If IsArray(data) Then
    Dim row
    Dim rows: rows = UBound(data, 2)
    For row = 0 To rows
        WScript.Echo data(2, row)
    Next
End If

输出:

fas plus-maintenicon
fas minus-suppicon
fas minus-affiicon

This is a serialised string where the record delimiter is | and the field delimiter is ~. You can easily build a 2-Dimensional Array from this using a double loop.

Dim str: str = "TR~Maintenance~fas plus-maintenicon|GR~Supplies~fas minus-suppicon|JK~Affidavit~fas minus-affiicon"

Dim data: data = ConvertToArray(str, "|", "~")
WScript.Echo data(1, 2)

Function ConvertToArray(value, record_delim, field_delim)
    Dim data()
    If Len(value) < 1 Then ConvertToArray = Empty
    Dim records: records = Split(value, record_delim)
    If Not IsArray(records) Then ConvertToArray = Empty
    Dim rows: rows = UBound(records)
    Dim row, col
    For row = 0 To rows
        Dim fields: fields = Split(records(row), field_delim)
        If Not IsArray(fields) Then ConvertToArray = Empty
        Dim cols: cols = UBound(fields)
        ReDim Preserve data(cols, row)
        For col = 0 To cols
            data(col, row) = fields(col)
        Next
    Next
    ConvertToArray = data
End Function

Output:

Affidavit

Using this function you can get the output you require by looping through and returning just the 3rd column from each row.

Dim data: data = ConvertToArray(str, "|", "~")
If IsArray(data) Then
    Dim row
    Dim rows: rows = UBound(data, 2)
    For row = 0 To rows
        WScript.Echo data(2, row)
    Next
End If

Output:

fas plus-maintenicon
fas minus-suppicon
fas minus-affiicon
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文