Check if String Contains, Replacing Sub Strings - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson describes the Contains and Replace methods

The String.Contains method is used when you want to check whether a specified substring occurs within the string.
It returns True if the value parameter occurs within the string, otherwise it returns False.

The String.Replace method is used when you want to replace all occurences of a specified string with another specified string.
It returns new string that is same as the current string except that all instances of oldValue are replaced with newValue.
If oldValue is not found this method returns the same string unchanged.

                    
                        Dim OriginalString As String = "Hello User"
                        Dim ModifiedString As String = String.Empty;
                        If OriginalString.Contains("Hello") Then
                            ModifiedString = OriginalString.Replace("Hello", "Good Bye")
                            Console.WriteLine(ModifiedString) ' = Good Bye User"
                        End If    
                                       
                    

Previous Lesson | Next Lesson


Share This

Subscribe