我仍然是dotNET的新手,我一直在搜索和搜索,并试图弄明白这一点。
我有一个字符串模式,如下所示:
3(a)-bab-4(c)-aab-7(d)-abab <---- pattern总是不同的,最多可以有50或60个字符
我需要的是:
aaababccccaabdddddddabab
有人可以帮我解决这个问题吗?
I am still very new to dotNET, and I have searched and searched, and racked my brain trying to figure this out.
I have a pattern as a string which looks like this :
3(a)-bab-4(c)-aab-7(d)-abab <---- pattern is always different and can be up to 50 or 60 characters
What I need is:
aaababccccaabdddddddabab
Can someone please help me figure this out?
最满意答案
这是一个可以使用您给出的示例的函数。 它使用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结果:
Here is a function that will work with the example that you gave. It is using String.Split and String.Replace to parse out your Data.
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 ModuleResult:
更多推荐
发布评论