C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Argument 1: In our Truncate example Function, the first argument is the source string. This is what we take a substring from.
Argument 2: This is a length. This equals the count of characters we are truncating our string to.
VB.NET program that truncates Strings
Module Module1
Public Function Truncate(value As String, length As Integer) As String
' If argument is too big, return the original string.
' ... Otherwise take a substring from the string's start index.
If length > value.Length Then
Return value
Else
Return value.Substring(0, length)
End If
End Function
Sub Main()
' Test the Truncate method with these two strings.
Dim test1 As String = "ABC"
Dim test2 As String = "ABCDEF"
Dim result = Truncate(test1, 2)
Console.WriteLine("2=" + result)
Console.WriteLine("4=" + Truncate(test2, 4))
Console.WriteLine("100=" + Truncate(test2, 100))
End Sub
End Module
Output
2=AB
4=ABCD
100=ABCDEF