Welcome to Asp.net, vb.net, C#.net Resources Sign in | Join | Help

ADO.Net

how to intercept the character string and restrict the character length using ASP.NET 2.0 and VB.NET 2005.
This tutorial will show you how to cut string using ASP.NET 2.0 and VB.NET 2005.

The tutorial use default namespace. The sampe 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


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"></asp:TextBox></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>


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

End Sub
End Class

 


This tutorial is provided in part by Mega Solutions Mega Solutions Inc.

Visit http://www.megasolutions.net for more ASP.NET Tutorials and Resources

Published Saturday, February 02, 2008 5:08 PM by admin

Filed under: , , , ,

Comments

No Comments
Anonymous comments are disabled
 
Powered by Community Server, by Telligent Systems