Strings in VB.NET

In this article I will explain you about Strings in VB.NET.
  • 1292
 

A string in VB.NET is simply a set of 16-bit bytes arranged in sequential memory locations. In other words, a string is simply an array of Unicode characters. VB.NET includes a variety of standard functions to recognize this data organization as a string and emulate the actions provided by operators on true string types in other languages. VB.NET prevents functions from writing outside the bounds of the string's memory area. The example given below is a Console application that shows some basic manipulation of strings. The program prompts the user for a string, a character to be found, and a character to replace the found characters. The program illustrates simple string initialization, assignment, concatenation and indexing.

Example of Replacing characters in a string


    // a string is an array of characters
    Class TheReplacer
        Shared s1 As String, s2 As String
        Shared
c2 As Char, c3 As Char
        Shared
Sub Main()
            Try
                System.Console.Write("Please enter a string:")
                s1 = System.
Console.ReadLine()
                System.
Console.Write("Please enter a character to be found:")
                Dim temp As String = System.Console.ReadLine()
                c2 = temp(0)
                System.
Console.Write("Please enter a character to replace the found chars:")
                temp = System.
Console.ReadLine()
                c3 = temp(0)
            Finally
            End
Try
            Try
                For
i As Integer = 0 To s1.Length - 1
                    If c2 = s1(i) Then
                        s2 += c3
                    Else
                        s2 += s1(i)
                    End If
                Next
            Finally
                System.Console.WriteLine(vbLf & "New string:" & s2)
                System.
Console.ReadLine()
            End Try
        End
Sub
    End
Class

The example above, has this output:

string.gif

Conclusion

Hope this article would have helped you in understanding Strings in VB.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.