gpt4 book ai didi

vb.net - 使用下划线作为标记分割字符串

转载 作者:行者123 更新时间:2023-12-02 19:14:27 27 4
gpt4 key购买 nike

自从我分割字符串以来已经有一段时间了,但是我需要使用“_”下划线作为分割字符串的标记来分割和重新排列文本。

例如:

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()

更新按照。您的评论:

I have a file name with a client number then the number that follows is a starting date and the last number is an end date. eg 1111_20140201_20140228. tom was here probably not a good example

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

关于vb.net - 使用下划线作为标记分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22149361/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com