自從我拆分字符串以來,它已經有一段時間了,但是我需要分割並重新排列文本,以便將字符串拆分爲標記「_」。使用下劃線作爲標記的分割字符串
如:
TOM_here_was
將隨即成爲
here_was_TOM
我怎麼做,在VB.net?
自從我拆分字符串以來,它已經有一段時間了,但是我需要分割並重新排列文本,以便將字符串拆分爲標記「_」。使用下劃線作爲標記的分割字符串
如:
TOM_here_was
將隨即成爲
here_was_TOM
我怎麼做,在VB.net?
排序的規則是什麼?
據分割,使用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
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
我會告訴你最簡單的方法。這是我會怎麼做的。首先,我們將把它作爲字符串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
我希望它會很好地工作!
我做了一個通用的使用功能爲您的問題:
''' <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
我會先被分割使用字符串「=」作爲分隔符,然後建立一個新的字符串一個不同的順序。 – Plutonix
訂購的規則是什麼? –
@ Tim作爲訂購規則,我有一個帶有客戶號碼的文件名,那麼隨後的號碼是開始日期,最後一個號碼是結束日期。例如1111_20140201_20140228。湯姆在這裏可能不是一個好例子 – Argon