Reverse string in VB.NET

Array is a collection of values of same data type and it can be referenced by same variable. The variables in array are called array elements and array elements are accessed through index name.
  • 5389
 

This article demonstrates to you how you can reverse string. I used array to reverse the string, when you click on button, array get string from textbox and reverse that string.

Array is a collection of values of same data type and it can be referenced by same variable. In simple term array is group of similar values. The variables in array are called array elements and array elements are accessed through index name. Element position of array is represented by index number.

img.gif 
ToCharArray function is used to converting string to their equivalent char array.

Array.Reverse method is used to reverse the order of all the elements in array. It is static method on the array type. I used Char array instead of string because it enable certain special memory optimizations.

Default.aspx

<%@ Page Title="Home Page"  StylesheetTheme="Theme1" Language="C#" MasterPageFile="~/Site.master
AutoEventWireup="true"
 CodeBehind="Default.aspx.cs" Inherits="Reverse._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<
asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button CssClass="btn" ID="idbtn" runat="server"  Text="Reverse" OnClick="btn1"/>
   <asp:Label ID="Label1" CssClass="lbl" Visible="False" runat="server" Text="Label"></asp:Label>
</asp:Content>

Default.aspx.vb

Partial Class _Default
    Inherits System.Web.UI.Page 
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load 
    End Sub 
    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Dim strArray As Char() = TextBox1.Text.ToCharArray()
        Array.Reverse(strArray)
        Dim strReversed As New String(strArray)
        Label1.Visible = True
Label1.Text = strReversed
        
    End Sub
End Class

StyleSheet.css

.btn
{
    margin-top: 80px;
    position: absolute;
    margin-left: -150px;
    background-color:Maroon;
    color:White;
    font-weight:bold;
   
 border-style:none;   
}

.lbl
{

    margin-top:80px;
    position:absolute;  
    margin-left:0px;
    width:auto;
    font-weight:bold;
    font-size:20px;
    color:White;
    background-color:Orange
}

Output:

output.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.