Trim Method
Trim removes leading and
trailing white space. String data often has leading or trailing white space characters—newlines,
spaces or tabs. These characters are usually not needed. With Trim we strip
these characters in a declarative way.
Eg:- Module Module1
Sub Main()
' Input string.
Dim value As String = " This is an example string. "
'
Invoke Trim function.
value = value.Trim()
' Write output.
Console.Write("[")
Console.Write(value)
Console.WriteLine("]")
End Sub
End Module
Output
[This is an
example string.]
Equals
VB.NET String
Equals function is to check the specified two String Object values are same or
not.
System.String.Equals(String str1, String
str2) As Boolean
Parameters:
String str1 : The String argument
String str2 : The String argument
Eg:-Dim str1 As
String = "Equals"
Dim str2 As String = "Equals"
If String.Equals(str1, str2) Then
MsgBox("Strings are Equal()
")
Else
MsgBox("Strings are not
Equal() ")
End If
Replace
and Insert Methods
Replace. This
changes the contents of a String. It makes all instances of one substring
become another substring. The Replace function on String is often useful.
Eg:- Module Module1
Sub Main()
' Assign string to literal.
Dim value1 As String = "XXPractical"
Console.WriteLine(value1)
' Replace substring with another
substring.
Dim value2 As String =
value1.Replace("XX", "Lab")
Console.WriteLine(value2)
End Sub
End Module
Output
XXPractical
LabPractical
StringBuilder:- This class has a Replace() that functions
the same way as the String method. It has parameters that indicate the before
and after substrings.
Eg:- Imports System.Text
Module Module1
Sub Main()
' Create new StringBuilder and
initialize it.
Dim builder As New
StringBuilder("This is an example.")
Console.WriteLine(builder)
' Replace string in StringBuilder.
builder.Replace("This",
"Here")
Console.WriteLine(builder)
' Insert into StringBuilder.
builder.Insert(0, "Sentence:
")
Console.WriteLine(builder)
End Sub
End Module
Output
This is an
example.
Here is an
example.
Sentence: Here
is an example.
Split
and Join
The VB.Net Split() extracts the substrings from the given string that are delimited by the separator parameter, and returns those substrings as elements of an array.
Public Function Split(ByVal ParamArray separator() As Char) As String()
Eg:- Dim str As String
Dim strArr() As String
Dim count As Integer
str = "vb.net split test"
strArr = str.Split(" ")
For count = 0 To strArr.Length - 1
MsgBox(strArr(count))
Next
Join combines many string elements into a single string. It acts on an array or List. By using the Join function, we collapse these collections into String instances. This can help with file handing, or for storage of data in a database.
Eg:-1. Module Module1
Sub Main()
' Three-element array.
Dim array(2) As String
array(0) = "A"
array(1) = "B"
array(2) = "C"
' Join array.
Dim result As String = String.Join(",", array)
' Display result.
Console.WriteLine(result)
End Sub
End Module
Output
A,B,C
0 comments
Post a Comment