String remove whitespace
The following asp.net c# example code demonstrate us how can we remove white space from a string object programmatically
at run time in an asp.net application. String represent a series of Unicode characters. String Class does not have any method
or property to identify white space/blank space in its value. So. at first we need to convert string value to Char objects.
String.ToCharAray() method allow us to copies the characters in a string instance to a Unicode character array. Then, we can loop through the characters array to determine which character is a white space. In this example, we loop through the char array using foreach loop.
Char.IsWhiteSpace(Char) overloaded method indicate whether a specified Unicode character is categorized as white space/empty space. So, by the way we can test each character of string that which represent white space. Then we can remove the specified characters from string object. Finally, we get a string where whitespaces are removed/deleted.
String.ToCharAray() method allow us to copies the characters in a string instance to a Unicode character array. Then, we can loop through the characters array to determine which character is a white space. In this example, we loop through the char array using foreach loop.
Char.IsWhiteSpace(Char) overloaded method indicate whether a specified Unicode character is categorized as white space/empty space. So, by the way we can test each character of string that which represent white space. Then we can remove the specified characters from string object. Finally, we get a string where whitespaces are removed/deleted.
string-remove-whitespace.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
//this section create a string variable.
string characters = "A B C D E F";
Label1.Text = "string of characters..................<br />";
Label1.Text += characters + "<br /><br />";
char[] chars = characters.ToCharArray();
characters = "";
foreach(char c in chars)
{
if (char.IsWhiteSpace(c))
{
Label1.Text += "whitespace";
}
else
{
Label1.Text += c.ToString();
characters += c;
}
Label1.Text += "<br />";
}
Label1.Text += "<br />after removing white spaces from string...........<br />";
Label1.Text += characters;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - string remove whitespace</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - string remove whitespace
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="string remove whitespace"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>

- How to format a decimal string as percentage value
- How to format a string as hexadecimal
- How to convert an integer to fixed length hex string format
- How to determine whether a string is equals to another string
- String equals case insensitive
- How to check whether a string ends with specific substring
- How to check whether a string starts with specific substring
- How to remove a character from a string at specified position
- How to remove a substring from a string
- How to remove the last character from a string