Description:
In this article, I want to show you how you can remove white spaces in a web page. The reality is that you can remove white spaces in any text file such as html, asp, aspx, txt, and so forth. this application is useful if your web site has limited bandwidth. here is the code which removes white spaces.
public void RemoveSpace()
{
char[] mychar=textBox1.Text.ToCharArray();
bFileSize=mychar.Length;
StringBuilder build= new StringBuilder();
char prev=' ';
foreach(char ch in mychar)
{
if(ch=='\r' || ch=='\n' || ch=='\t')
{
}
else
{
if(prev==' ' && ch==' ')
{
}
else
{
build.Append(ch);
}
prev=ch;
}
}
textBox2.Text=build.ToString();
aFileSize=textBox2.Text.Length;
}
as you can see from the code above, we firstly eliminate the characters of tab, endofline, and then we check if there are more whitespaces between characters or not. if not, we append all these charaters to a variable of StringBuilder data type. anyway, during that, we eliminate all the spaces in the web page or any text file. I attached an example, it shows how you can use the application.
first thing is that, you need to open a web page from File menu--->Open---> File
in the following figure, we open Index.aspx to optimize, then click on Open button
when you click on Open button, the application reads text from choosen file and assign it to the textbox control. as you see in the following figure
now, it is time to click on Optimize button, as you see we save 17.35% from file size. so ,you will be able to server more visitors with the same bandwidth.
I hope it will be useful in your web programs. if you have any questions, do not hesitate to ask me.
Cheers!!