Sometimes we come across situation where we need to remove the whitespace whithin the string.
So here is the simple code for the same.
Step 1: Markup of the page
<asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine" Columns="20"
Width="300px" style="height: 300px"></asp:TextBox>
<br />
<asp:Button ID="btnCheckAddress" runat="server" Text="Validate Space"
onclick="btnCheckAddress_Click" />
Step 2: Code
protected void btnCheckAddress_Click(object sender, EventArgs e)
{
txtAddress.Text= txtAddress.Text.Trim();//To clear leading and trailing space
// Following loop is to remove the whitespace within the string
while (txtAddress.Text.Contains(" "))/Here Contains method check for 2 continues space
{
txtAddress.Text = txtAddress.Text.Replace(" ", " ");//Here Replace method will replace 2 continues space with single space
}
}
Output:
So here is the simple code for the same.
Step 1: Markup of the page
<asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine" Columns="20"
Width="300px" style="height: 300px"></asp:TextBox>
<br />
<asp:Button ID="btnCheckAddress" runat="server" Text="Validate Space"
onclick="btnCheckAddress_Click" />
Step 2: Code
protected void btnCheckAddress_Click(object sender, EventArgs e)
{
txtAddress.Text= txtAddress.Text.Trim();//To clear leading and trailing space
// Following loop is to remove the whitespace within the string
while (txtAddress.Text.Contains(" "))/Here Contains method check for 2 continues space
{
txtAddress.Text = txtAddress.Text.Replace(" ", " ");//Here Replace method will replace 2 continues space with single space
}
}
Output:
gud one bro.......................
ReplyDeleteThanks..
Delete