我很尴尬地分享这个功能,但我需要帮助整理它。很久以前我写这篇文章只是为了一个非常简单的用法,但是它已经失控了,我不确定如何正确处理它。
Public Shared Function SetVariables(msg As String, Optional name As String = "", Optional target As String = "", Optional amount As Decimal = 0, Optional cost As String = "0", Optional keyword As String = "", Optional time As String = "", Optional reward As String = "", Optional participantList As String = "", Optional participantCount As Integer = 0, Optional game As String = "", Optional viewers As String = "", Optional followers As String = "", Optional link As String = "", Optional _options As String = "", Optional Year As String = "", Optional Month As String = "", Optional Day As String = "", Optional Hour As String = "", Optional Minute As String = "", Optional grpname As String = "")
Dim balance As Decimal
Dim holdings As Decimal
If name > "" Then
If Options.Accounts.ContainsKey(name) Then
If Options.Holdings.ContainsKey(Options.Accounts.Item(name)) Then
holdings = Options.Holdings.Item(Options.Accounts.Item(name))
End If
balance = Options.Accounts.Item(name).Points
End If
End If
msg = msg.Replace("[name]", StrConv(name, VbStrConv.ProperCase))
If holdings > 0 Then
msg = msg.Replace("[balance]", balance & "[" & holdings & "]")
Else
msg = msg.Replace("[balance]", balance)
End If
msg = msg.Replace("[channel]", Subs.UppercaseFirstLetter(Options.Channel.TrimStart("#")))
msg = msg.Replace("[target]", Subs.UppercaseFirstLetter(target))
msg = msg.Replace("[amount]", amount)
msg = msg.Replace("[cost]", cost)
msg = msg.Replace("[keyword]", keyword)
msg = msg.Replace("[time]", time)
msg = msg.Replace("[reward]", reward)
msg = msg.Replace("[participantList]", participantList)
msg = msg.Replace("[participantCount]", participantCount)
msg = msg.Replace("[botname]", Subs.UppercaseFirstLetter(Options.User))
If msg.Contains("[groups]") Then msg = msg.Replace("[groups]", GetSortedGroups(name))
If msg.Contains("[group]") Then msg = msg.Replace("[group]", GetSortedGroups(name, True))
msg = msg.Replace("[game]", StrConv(game, VbStrConv.ProperCase))
msg = msg.Replace("[viewers]", viewers)
msg = msg.Replace("[followers]", followers)
msg = msg.Replace("[link]", link)
msg = msg.Replace("[options]", options.ToUpper)
msg = msg.Replace("[years]", Year)
msg = msg.Replace("[months]", Month)
msg = msg.Replace("[days]", Day)
msg = msg.Replace("[hours]", Hour)
msg = msg.Replace("[minutes]", Minute)
msg = msg.Replace("[grpname]", StrConv(grpname, VbStrConv.ProperCase))
If balance = 1 Or amount = 1 Then
msg = msg.Replace("[currency]", Options.PName)
Else
msg = msg.Replace("[currency]", Options.PNames)
End If
Return msg
End Function
基本上我将一个字符串传递给这个函数,它包含一些:[name] [keyword]等,用其他东西替换。有时我还必须传递数据以替换那些问题开始的地方。我希望这些功能在同一个功能中,但我现在可以使用很多参数了。我从不在函数的单次调用中使用所有这些参数,随着时间的推移,我将增加更多的参数。
关于如何更好地处理这样的事情的任何建议?我应该拆开这个功能并单独处理更换吗?
答案 0 :(得分:1)
你这样做的方式相当昂贵。 Strings
是不可变的,所以这样的一行:
msg = msg.Replace("[followers]", followers)
...撕开原来的msg
,然后从碎片和替换品中创建一个新的。我已经完成了用户创建的用于指定文本块布局的短字符串的确切内容,但StringBuilder
对于更长的字符串和/或大量替换将更快更高效。 This post is an extreme example使用1MB字符串(SB将时间从5分钟切换到86毫秒)。
因为听起来你构造了一个被切断的起始字符串,如果可能的话,我会尝试从头开始构建它并沿途格式化它。我对数据或其他一些方法知之甚少,但这应该给你一个想法:
Public Class MessageMaker
Public Property Name As String
Public Property Target As String
Public Property Amount As Nullable(Of Decimal)
Public Property Cost As String ' string? Really?
' illustration
Public Property Participants As List(Of String)
' ergo participantCount==Participants.COunt()
Public Property GroupName As String
' etc ad nauseum
Public Sub New()
Participants = New List(Of String)
End Sub
Public Function GetFormattedMsg() As String
Dim sb As New StringBuilder
sb.AppendFormat("The Name: {0}; ", Name)
' or...this will only append the name when lengh>0
'sb.AppendFormat(If(String.IsNullOrEmpty(Name), "", TitleCase(Name) & "; "))
If Amount.HasValue Then
sb.AppendFormat("amt = {0}; ", Amount.Value.ToString("C2"))
End If
Dim p As String = ""
If Participants.Count > 0 Then
sb.AppendFormat("Participant Count: {0}; ", Participants.Count)
' convert names to TitleCase, sort
p = String.Join(", ", Participants.OrderBy(Function(x) x).
Select(Function(j) TitleCase(j)))
sb.AppendFormat("Participant Names: {0}; ", p)
End If
sb.Append(If(String.IsNullOrEmpty(GroupName), "",
String.Format("Grp: {0}; ", TitleCase(GroupName))))
Return sb.ToString
End Function
Private Function TitleCase(str As String) As String
Return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower)
End Function
End Class
请注意,Amount
为Nullable(Of Decimal)
(或可写为As Decimal?
)。如果您想省略它,除非给出它,您可以使用.HasValue
来确定。这可以防止误导数字:Amount: 0
是真的意味着0还是意味着它没有被指定。除非重要,否则不要打扰Nullable<T>
。
GroupName
处理显示了如何有条件地添加文字。我还将旧式StrConv
替换为.NET方法。我不知道GetSortedGroups
做了什么,但是如果你想要的话,任何组列表都可以动态排序(如Participants
所示)。
根据此类需要做的其他事情而不是方法,结果可能来自.ToString()
:
Public Overrides Function ToString() As String
' all the code
Return msg
End Function
测试代码:
Dim mm As New MessageMaker
mm.Name = "April Gala Festival"
mm.Amount = 1.23D
mm.Participants = New List(Of String) From {"ziggy", "zOEy", "HOOveR", "josh"}
Dim msg = mm.GetFormattedMsg()
' or
Dim msg = mm.ToString()
结果:
“名称:四月联欢节; amt = $ 1.23;参与者人数:4;参与者姓名:Hoover,Josh,Ziggy,Zoey;”
我猜测结果字符串有一些标题和分隔符。该方法在每个段或元素之后使用"; "
。