Search This Blog

Monday, April 4, 2011

convert multiple space in one space by function in c#

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Text.RegularExpressions;
public partial class SMS_space : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    public static string NormalizeWhiteSpace(string S)
    {
        string s = S.Trim();
        bool iswhite = false;
        int iwhite;
        int sLength = s.Length;
        StringBuilder sb = new StringBuilder(sLength);
        foreach (char c in s.ToCharArray())
        {
            if (Char.IsWhiteSpace(c))
            {
                if (iswhite)
                {
                    continue;
                }
                else
                {
                    sb.Append(" ");
                    iswhite = true;
                }
            }
            else
            {
                sb.Append(c.ToString());
                iswhite = false;
            }
        }
        return sb.ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //*********************its also working**********************************
        //string text = NormalizeWhiteSpace(TextBox1.Text);
        //Response.Write(text);
        //******************************8
        Regex r = new Regex(@"\s+");
        // Strip multiple spaces.
        string s3 = r.Replace(TextBox1.Text, @" ");
        Response.Write(s3);    
    }
}

No comments :