StartsWith and EndsWith method in VB.NET

String.StartsWith function is use to compare starting value from other string and it returns value is Boolean type. EndsWith method check string from the end point.
  • 5077
Download Files:
 

String.StartsWith function is use to compare starting value from other string and it returns value is Boolean type. It is mostly use to check whether the string starts from specified character or not. Overload function of StartsWith enable you to match the string with case sensitive or insensitive. StartsWith with function has 3 overload methods.

  1. Public Function StartsWith (value As String) As Boolean End Function
  2. Public Function StartsWith (value As String, comparison Type As StringComparison) As Boolean End Function
  3. Public Function StartsWith (value As String, ignoreCase As Boolean, culture As CultureInfo) As Boolean End Function
Same as StartsWith method, EndsWith method works but its check other string from end. If specified character exists at the end then it will return true value. It also returns value in Boolean type as True or False.

Code

Default.aspx

<%
@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"    CodeFile="Default.aspx.vb" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<
asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div style="height: 500px; width: 859px">
        <br />
        <br />
        <br />
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" Text="Schwarzenegger" Font-Bold="True" Style="top: 176px;
             left: 130px; position: absolute; height: 17px; width: 104px"></asp:Label>
        Use the above string to know how StartsWith and EndsWith method works.<br />
        <br />        " Start With"        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" />
        <br />
        <br />
        " End With"
        <asp:TextBox ID="textbox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button2" runat="server" Text="Search" OnClick="Button2_Click" />
        <br />
        <br />
        <br />    
        <asp:Label ID="Label1" runat="server" Text="Schwarzenegger" Font-Bold="True" Style="top: 176px;
             left: 130px; position: absolute; height: 17px; width: 104px"></asp:Label>
    </div>
</asp:Content>

Default.aspx.vb

Imports System.Collections.Generic
Imports System.Windows.Forms
Partial Class _Default
    Inherits System.Web.UI.Page 
    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        If Label1.Text.StartsWith(TextBox1.Text) Then
             MessageBox.Show(TextBox1.Text, "Found")
        Else
              MessageBox.Show(textbox2.Text, "Not Found")
        End If
    End Sub 
    Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click
        If Label1.Text.EndsWith(textbox2.Text) Then
             MessageBox.Show(textbox2.Text, "Found")
        Else 
            MessageBox.Show("Not Found")
        End If
    End Sub
End Class

Write in "Sc" in first textbox as looking in below.

Startswith.gif


Click on Search button. This button compare your "Sc" value is exist in starting point of  Schwarzenegger. If value match then it will show you message box, see below.

StartsWith-method.gif


Write something in textbox that should not similar to Schwarzenegger and then click on search button you will get message box that show string not found see below.


Starts-method.gif

Same do in second textbox. Write in "r" in second textbox as looking in below image.

r.gif


Click on Search button. This button compare your "r" value is exist in ending point of Schwarzenegger. If value match then it will show you message box, see below.

EndsWith.gif


Write something in textbox that should not similar to Schwarzenegger and then click on search button you will message box that show string not found see below.

endwith.gif

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.