我以以下格式输入了字符串
"[1_5,3,7,1],[1_2,4,1,9],[],[1_1,,4,,,9,2]"
我需要获取的是相同的字符串,但_
排序后的数字是:
"[1_1,3,5,7],[1_1,2,4,9],[],[1_1,2,4,9,,,]"
Dim tmprequestedArea_selectionAreaIn As String = "[1_5,3,7,1],[1_2,4,1,9],[],[1_1,,4,,,9,2]"
tmprequestedArea_selectionAreaIn = Regex.Replace(requestedArea_selectionAreaIn,"\],\[","#")
tmprequestedArea_selectionAreaIn = Regex.Replace(tmprequestedArea_selectionAreaIn,"\[|\]","")
bracList.AddRange(tmprequestedArea_selectionAreaIn.Split(New Char() {"#"c}, StringSplitOptions.None ))
If sortNumber Then
'Split braclist by _ and puts the value in strList
'If after _ is only one number put only that number, else split it by char "," and put in strList the join of the split by , array
'Sort the array
'in previous example strList will contain a,b,c in position 0 and _d_f (instead of f,d) in position 1
For i As Integer = 0 To bracList.Count -1
Dim tmp As String()
Dim tmpInt As New System.Collections.Generic.List(Of Integer)
If Not(String.IsNullOrEmpty(bracList(i))) Then
Dim tmpRequested As String = bracList(i).Split(New Char() {"_"c})(0)
Dim tmpSelection As String = bracList(i).Split(New Char() {"_"c})(1)
If tmpSelection.Contains(",") Then
tmp = tmpSelection.Split(New Char() {","c})
For j As Integer = 0 To tmp.Length -1
tmpInt.Add(Convert.toInt32(tmp(j)))
Next
tmpInt.Sort
strList.Add("[" + tmpRequested + "_" + String.Join(",",tmpInt ) + "]")
Else
strList.Add("[" + tmpRequested + "_" + tmpSelection + "]" )
End If
Else
strList.Add("[]")
End If
Next i
我正在寻找一种更好的管理方式。
尝试一下,以替代您现在正在做的事情。
给定此输入字符串:
Dim input As String = "[1_5,3,7,1],[1_2,4,1,9],[],[1_1,,4,,,9,2]"
注意:这还将处理不更改的十进制值。例如,
"[1_5.5,3.5,7,1],[1_2.564,4,2.563,9],[],[1_1,,4.23,,,9.0,2.45]"
您可以使用以下模式提取方括号的内容:\[(.*?)\]
并使用Regex.Matches返回匹配该模式的所有子字符串的MatchCollection。
然后,在处理零件时,使用StringBuilder作为容器来重建字符串。
Imports System.Linq
Imports System.Text.RegularExpressions
Dim pattern As String = "\[(.*?)\]"
Dim matches = Regex.Matches(input, pattern, RegexOptions.Singleline)
Dim sb As StringBuilder = New StringBuilder()
For Each match As Match In matches
Dim value As String = match.Groups(1).Value
If String.IsNullOrEmpty(value) Then
sb.Append("[],")
Continue For
End If
Dim sepPosition As Integer = value.IndexOf("_"c) + 1
sb.Append("[" & value.Substring(0, sepPosition))
Dim values = value.Substring(sepPosition).Split(","c)
sb.Append(String.Join(",", values.Where(Function(n) n.Length > 0).OrderBy(Function(n) CDec(n))))
sb.Append(","c, values.Count(Function(n) n.Length = 0))
sb.Append("],")
Next
Dim result As String = sb.ToString().TrimEnd(","c)
如果您不了解LINQ,这就是它的作用:
String.Join(",", values.Where(Function(n) n.Length > 0).OrderBy(Function(n) CDec(n)))
values
是由生成的字符串数组String.Split()
。
values.Where(Function(n) n.Length > 0)
:创建一个Enumerable(Of String)
从values
Where
的内容,n
是长度的串> 0
。
我可以写values.Where(Function(n) Not String.IsNUllOrEmpty(n)
)。
.OrderBy(Function(n) CDec(n)))
:Enumerable(Of String)
使用转换为Decimal的字符串值对结果进行排序,并生成一个Enumerable(Of String)
,并将其传递回String.Join()
,以重建字符串,并","c
在各部分之间添加一个char()。
values.Count(Function(n) n.Length = 0)
:计算values
具有Lenght = 0
(空字符串)的元素。这是用逗号表示的空元素的数量,附加在部分字符串的末尾。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句