首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在VB.net中解压缩模式

在VB.net中解压缩模式
EN

Stack Overflow用户
提问于 2014-04-14 02:46:02
回答 2查看 62关注 0票数 3

我对dotNET还是很陌生的,我已经搜索过了,并且绞尽脑汁想弄清楚这一点。

我有一个字符串的模式,如下所示:

3(a)-bab-4(c)-aab-7(d)-abab -aab-7(D)型

我需要的是:

aaababccccaabdddddddabab

有人能帮我弄清楚吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-14 03:19:31

下面是一个函数,它将与您给出的示例一起工作。它使用String.Split和String.Replace来解析数据。

代码语言:javascript
复制
Module Module1

    Sub Main()
        Dim testValue As String = "3(a)-bab-4(c)-aab-7(d)-abab"
        Console.WriteLine(testValue)
        Console.WriteLine(ParseData(testValue))
        Console.ReadLine()
    End Sub

    Public Function ParseData(value As String) As String
        Dim temp As String = ""
        Dim result As String = ""
        Dim splitChar As Char() = {"-"c}
        Dim split() As String = value.Split(splitChar, StringSplitOptions.RemoveEmptyEntries)
        For Each section In split
            If (IsNumeric(section(0))) Then 'Check to see if section starts with a number
                Dim tmpvalue1() As String = section.Split(New Char() {"("c}) 'Check if section contains a paren
                If tmpvalue1.Length > 0 Then 'If so then replace them and strip out the number to get to the repeating characters
                    Dim validChar As String = section.Replace("(", "").Replace(")", "").Replace(tmpvalue1(0), "")
                    Dim count As Integer
                    If Integer.TryParse(tmpvalue1(0), count) Then
                        temp = validChar
                        For x = 0 To count - 2
                            temp += validChar
                        Next
                    End If
                End If
                result += temp
            Else
                result += section
            End If

        Next
        Return result
    End Function

End Module

结果:

票数 1
EN

Stack Overflow用户

发布于 2014-04-14 02:55:59

它看起来像一个RLE,将字符串拆分为'-',然后检查是否包含‘(如果是,取数字,提取文本并在循环中重复

代码语言:javascript
复制
Dim sb As New StringBuilder()

Dim parts As String() = inputString.Split("-"C)

For Each part As String In parts

    Dim indexOfPar As Integer = part.IndexOf("(")

    If indexOfPar = -1 Then
        sb.Append(part)
    Else
        Dim repeat As Integer = Integer.Parse(part.Substring(0, indexOfPar))

        Dim toRepeat As String = part.Substring(indexOfPar, part.Length - (indexOfPar - 2))
        '-2 to remove ()
        For buc As Integer = 0 To repeat - 1
            sb.Append(toRepeat)
        Next

    End If

Next

Return sb.ToString()
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23051166

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档