Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Advanced Technologies - Cut String by ASP.NET 2.0 and C#
Cut String by ASP.NET 2.0 and C#


ASP.NET Advanced Technologies Tutorial

This tutorial will show you how to cut string using ASP.NET 2.0 and C#.

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 void btn_cut_Click(object sender, EventArgs e)
{
string s="";

string str = string.Empty;
str = txtInputString.Text.Trim();

int len = 0;
if (txt_length.Text.Equals("") == false)
len = Int32.Parse(txt_length.Text.Trim());

for (int i = 0; i < str.Length; i++)
{
int r = i % len;
int last = (str.Length / len) * len;
if (i != 0 && i <= last)
{
if (r == 0)
{
s += str.Substring(i - len, len) + "<br>";
}
}
else if (i > last)
{
s += str.Substring(i - 1);
break;
}
}
this.lblMessage.Text = s;
}

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

The front end CutStringCsharp.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>

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

The flow for the code behind page is as follows

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
// 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
}

protected void btn_cut_Click(object sender, EventArgs e)
{
string s="";

string str = string.Empty;
str = txtInputString.Text.Trim();

int len = 0;
if (txt_length.Text.Equals("") == false)
len = Int32.Parse(txt_length.Text.Trim());

for (int i = 0; i < str.Length; i++)
{
int r = i % len;
int last = (str.Length / len) * len;
if (i != 0 && i <= last)
{
if (r == 0)
{
s += str.Substring(i - len, len) + "<br>";
}
}
else if (i > last)
{
s += str.Substring(i - 1);
break;
}
}
this.lblMessage.Text = s;
}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks