自从我分裂字符串以来已经有一段时间了,但是我需要将“_”下划线作为标记分割字符串进行拆分和重新排列。
例如:
TOM_here_was
然后将成为
here_was_TOM
我怎么在VB.net中这样做?
答案 0 :(得分:1)
订购的规则是什么?
根据拆分,使用Split("_"c)
获取数组:
Dim tokens = "TOM_here_was".Split("_"c)
现在你拥有所有部分,如果你想要一个随机顺序(例如,因为它不清楚):
tokens = tokens.OrderBy(Function(s) Guid.NewGuid()).ToArray()
更新 acc。你的评论:
我的文件名带有客户编号,然后是后面的编号 是一个开始日期,最后一个数字是结束日期。例如 1111_20140201_20140228。汤姆在这里可能不是一个好例子
Dim path = "C:\Temp\1111_20140201_20140228.txt"
Dim fileName = System.IO.Path.GetFileNameWithoutExtension(path)
Dim tokens = fileName.Split("_"c)
If tokens.Length = 3 Then
Dim client = tokens(0)
Dim startDate, endDate As Date
Dim parsableStart = Date.TryParseExact(tokens(1),
"yyyyMMdd",
Globalization.CultureInfo.InvariantCulture,
Globalization.DateTimeStyles.None,
startDate)
Dim parsableEnd = Date.TryParseExact(tokens(2),
"yyyyMMdd",
Globalization.CultureInfo.InvariantCulture,
Globalization.DateTimeStyles.None,
endDate)
If parsableStart AndAlso parsableEnd Then
Console.WriteLine("Client: {0} Start: {1} End: {2}", client, startDate, endDate)
End If
End If
如果要在目录中订购文件,可以使用LINQ:
Dim startDate, endDate As Date
Dim fileNames = System.IO.Directory.EnumerateFiles("C:\Temp\", "*.*", SearchOption.TopDirectoryOnly)
Dim orderedFilenames =
From path In fileNames
Let fileName = System.IO.Path.GetFileNameWithoutExtension(path)
Let tokens = fileName.Split("_"c)
Where tokens.Length = 3
Let client = tokens(0)
Let startDateParsable = Date.TryParseExact(tokens(1), "yyyyMMdd", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, startDate)
Let endDateparsable = Date.TryParseExact(tokens(2), "yyyyMMdd", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, endDate)
Where startDateParsable AndAlso endDateparsable
Order By startDate, endDate
Select New With { fileName, client, startDate, endDate }
For Each fn In orderedFilenames
Console.WriteLine("File: {0} Client: {1} Start: {2} End: {3}", fn.fileName, fn.client, fn.startDate, fn.endDate)
Next
答案 1 :(得分:0)
Dim myString = "TOM_here_was"
Dim splitArray() As String
splitArray = Split(myString, "_", -1)
在此示例中,splitArray()将具有以下值:
之后,您可以使用splitArray创建一个新的字符串。
由于您尚未指定如何重新组织新字符串,除了执行以下操作之外,我无法提供帮助:
Dim newString = splitArray[0] & "_" & splitArray[2] & "_" & splitArray[1]
得到:TOM_was_here
答案 2 :(得分:0)
我会告诉你最简单的方法。我就是这样做的。首先,我们将其作为字符串test
,我们将使用test.Split()
并指定从下划线拆分。然后将拆分部分存储在变量parts
。
我们将在Label1
中将他们加在一起(您可以在变量或任何您想要的地方进行)。
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim test As String = "TOM_Here_was"
Dim parts As String() = test.Split("_"c)
If parts.Length >= 3 Then
Label1.Text = parts(1) & "_" & parts(2) & "_" & parts(0)
End If
End Sub
End Class
我希望它能完美运作!
答案 3 :(得分:0)
我为您的问题制作了通用的使用函数:
''' <summary>
''' Splits an String and rotates an amount of splitted tokens.
''' </summary>
''' <param name="String">Indicates the string to split and rotate.</param>
''' <param name="Delimiter">Indicates the delimiter to split.</param>
''' <param name="Rotation">Indicates the rotation count.</param>
''' <returns>System.String.</returns>
''' <exception cref="Exception">Rotation index out of range.</exception>
Private Function SplitAndRotate(ByVal [String] As String,
ByVal Delimiter As Char,
ByVal Rotation As Integer) As String
Dim Parts As String() = [String].Split(Delimiter)
If Rotation >= Parts.Length Then
Throw New Exception("Rotation index out of range.")
End If
Return String.Format("{0}{1}",
String.Join(Delimiter,
From s As String In Parts Skip Rotation) & CStr(Delimiter),
String.Join(Delimiter,
From s As String In Parts Take Rotation))
End Function
用法:
Dim str As String = SplitAndRotate("TOM_here_was", "_"c, 1)
' Result: here_was_TOM