Conrad Jagger
Conrad Jagger

Reputation: 643

vb.net - passing string to function and replacing few words in that string

Please help, I need to pass string to function with few optional parameters which needs to be replaced within string. Can someone help how can this be done, as im not to vb.net

String - "The <field1> is mandatory"
Variable - Employee Id
Output should be - "The Employee Id is mandatory"

Regards

Upvotes: 0

Views: 6743

Answers (2)

Mark Hall
Mark Hall

Reputation: 54562

You would be looking at the String.Replace method. something like this. Since strings are immutable it returns a new string with your corrected value you would then need to assign it to the old string.

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

You could also use the String.Format Method which uses composite formatting which will allow you to embed fields in your string then replace them with variables. something like this.

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)

Upvotes: 2

user1954492
user1954492

Reputation: 495

You can use this

Public Function cal(Byref id)
Dim str as String
str= "The "+id+" is mandatory"
return str
End Function

and call this function by

s=cal(id) //id is the value to be passed and s is the result

Upvotes: 1

Related Questions