Tuesday, 19 July 2011

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

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

The front end CutStringVB.aspx page looks something like this:

<div align="left" style="text-align: center">
&nbsp;<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>
&nbsp;&nbsp;
</div>

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

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.AspNetTutorials.com for more ASP.NET Tutorials
End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment