请帮忙,我需要将字符串传递给函数,其中包含几个需要在字符串中替换的可选参数。有人可以帮助如何做到这一点,因为我不是 vb.net
String - "The <field1> is mandatory"
Variable - Employee Id
Output should be - "The Employee Id is mandatory"
问候
请帮忙,我需要将字符串传递给函数,其中包含几个需要在字符串中替换的可选参数。有人可以帮助如何做到这一点,因为我不是 vb.net
String - "The <field1> is mandatory"
Variable - Employee Id
Output should be - "The Employee Id is mandatory"
问候
您将查看String.Replace 方法。像这样的东西。由于字符串是不可变的,因此它会返回一个带有更正值的新字符串,然后您需要将其分配给旧字符串。
Module Module1
Sub Main()
Dim test As String = "The <field1> is mandatory"
Dim variable As String = "Employee Id"
test = test.Replace("<field1>", variable)
Console.WriteLine(test)
Console.ReadLine()
End Sub
End Module
您还可以使用使用复合格式的String.Format方法,该方法允许您在字符串中嵌入字段,然后用变量替换它们。像这样的东西。
Dim test As String = "The {0} is a mandatory {1}"
Dim variable As String = "Employee Id"
Dim variable2 As String = "Field"
test = String.Format(test, variable, variable2)
你可以用这个
Public Function cal(Byref id)
Dim str as String
str= "The "+id+" is mandatory"
return str
End Function
并通过调用此函数
s=cal(id) //id is the value to be passed and s is the result