

Navigator : Home > Tutorials > Advanced Technologies Tutorials > ...
Cut String by ASP.NET 2.0 and VB.NET
This tutorial will show you how to cut string using ASP.NET 2.0 and VB.NET 2005.
This tutorial uses default namespace. The sample will show you how to intercept the character string and restrict the character length. Moreover, it will automatically turn to the newline.
In order to run the example, we will use the btn_cut_Click to trigger the task. The code as following:
Protected Sub btn_cut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_cut.Click
Dim s As String = "" Dim str As String = String.Empty str = txtInputString.Text.Trim() Dim len As Integer = 0 If txt_length.Text.Equals("") = False Then
len = Int32.Parse(txt_length.Text.Trim()) End If For i As Integer = 0 To str.Length
Dim r As Integer = i Mod len Dim last As Integer = (str.Length / len) * len If i <> 0 And i <= last Then
If r = 0 Then
s += str.Substring(i - len, len) + "<br>" End If ElseIf i > last Then
s += str.Substring(i - 1) Exit For End If next Me.lblMessage.Text = s End Sub |
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
The front end CutStringVB.aspx page looks something like this:
<div align="left" style="text-align: center"> <table>
<tr>
<td colspan="2" style="width: 200px"> Input String :</td> <td colspan="1" style="width: 17px"> <asp:TextBox ID="txtInputString" runat="server"></td> </tr> <tr>
<td colspan="2" style="height: 26px"> Length:</td> <td colspan="1" style="width: 17px"> <asp:TextBox ID="txt_length" runat="server"></asp:TextBox></td> </tr> <tr>
<td colspan="3" style="height: 26px"> <asp:Button ID="btn_cut" runat="server" Text="Cut String" OnClick="btn_cut_Click"/></td> </tr> <tr>
<td colspan="3" style="height: 26px"> <asp:Label ID="lblMessage" runat="server" ForeColor="Red"></asp:Label></td> </tr> </table> </div> |
The flow for the code behind page is as follows
|
Partial Class _Default
Inherits System.Web.UI.Page Protected Sub btn_cut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_cut.Click
Dim s As String = "" Dim str As String = String.Empty str = txtInputString.Text.Trim() Dim len As Integer = 0 If txt_length.Text.Equals("") = False Then
len = Int32.Parse(txt_length.Text.Trim()) End If For i As Integer = 0 To str.Length
Dim r As Integer = i Mod len Dim last As Integer = (str.Length / len) * len If i <> 0 And i <= last Then
If r = 0 Then
s += str.Substring(i - len, len) + "<br>" End If ElseIf i > last Then
s += str.Substring(i - 1) Exit For End If next Me.lblMessage.Text = s End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' This tutorial is provided in part by Server Intellect Web Hosting Solutions http://www.serverintellect.com ' Visit http://www.DotNetTutorials.com for more ASP.NET Tutorials End Sub End Class |
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
Looking for the C#.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!