我对dotNET还是很陌生的,我已经搜索过了,并且绞尽脑汁想弄清楚这一点。
我有一个字符串的模式,如下所示:
3(a)-bab-4(c)-aab-7(d)-abab -aab-7(D)型
我需要的是:
aaababccccaabdddddddabab
有人能帮我弄清楚吗?
发布于 2014-04-14 03:19:31
下面是一个函数,它将与您给出的示例一起工作。它使用String.Split和String.Replace来解析数据。
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结果:

发布于 2014-04-14 02:55:59
它看起来像一个RLE,将字符串拆分为'-',然后检查是否包含‘(如果是,取数字,提取文本并在循环中重复
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()https://stackoverflow.com/questions/23051166
复制相似问题