ASPDOTNET C# STRING MANIPULATION EXAMPLES




















- <%@ 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 stringValue = "red green Red yellow darkRed";
- Label1.Text = "string..................<br />";
- Label1.Text += stringValue.ToString();
- //this section replace substring with substring in string ignore case.
- string replacedString = Regex.Replace(stringValue, "red", "blue", RegexOptions.IgnoreCase);
- Label1.Text += "<br /><br />modified string......................";
- Label1.Text += "<br />" + replacedString.ToString();
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>c# example - string replace ignore case</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:MidnightBlue; font-style:italic;">
- c# example - string replace ignore case
- </h2>
- <hr width="550" align="left" color="Gainsboro" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="Large"
- Font-Bold="true"
- >
- </asp:Label>
- <br /><br />
- <asp:Button
- ID="Button1"
- runat="server"
- Text="string replace ignore case"
- OnClick="Button1_Click"
- Height="40"
- Font-Bold="true"
- />
- </div>
- </form>
- </body>
- </html>


- <%@ Page Language="C#" AutoEventWireup="true"%>
- <!DOCTYPE html>
- <script runat="server">
- protected void Button1_Click(object sender, System.EventArgs e)
- {
- //this section create string variables.
- string stringValue = "1 2 3 4 5 6 7 8 9 10";
- string stringValue2 = "replacing multiple characters in a string.";
- Label1.Text = "string1: " + stringValue.ToString();
- Label1.Text += "<br />string2: " + stringValue2.ToString();
- //this section replace charcters with character in string.
- string modifiedString1 = stringValue.Replace(' ', '-');
- string modifiedString2 = stringValue2.Replace('a', 'k');
- Label1.Text += "<br /><br />modified string 1......................";
- Label1.Text += "<br />" + modifiedString1.ToString();
- Label1.Text += "<br /><br />modified string 2......................";
- Label1.Text += "<br />" + modifiedString2.ToString();
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>c# example - string replace character</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:MidnightBlue; font-style:italic;">
- c# example - string replace character
- </h2>
- <hr width="550" align="left" color="Gainsboro" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="Large"
- Font-Bold="true"
- >
- </asp:Label>
- <br /><br />
- <asp:Button
- ID="Button1"
- runat="server"
- Text="string replace character"
- OnClick="Button1_Click"
- Height="40"
- Font-Bold="true"
- />
- </div>
- </form>
- </body>
- </html>


- <%@ Page Language="C#" AutoEventWireup="true"%>
- <!DOCTYPE html>
- <script runat="server">
- protected void Button1_Click(object sender, System.EventArgs e)
- {
- //this line create a string variable.
- string stringValue = "how is it possible to replace a string in a string?";
- Label1.Text = "string......................";
- Label1.Text += "<br />string: " + stringValue.ToString();
- string replaceWith = "impossible";
- //this line replace 'possible' substring with 'impossible' in string.
- string modifiedString = stringValue.Replace("possible", replaceWith);
- Label1.Text += "<br /><br />modified string......................";
- Label1.Text += "<br />" + modifiedString.ToString();
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>c# example - string replace</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:MidnightBlue; font-style:italic;">
- c# example - string replace
- </h2>
- <hr width="550" align="left" color="Gainsboro" />
- <asp:Label
- ID="Label1"
- runat="server"
- Font-Size="Large"
- Font-Bold="true"
- >
- </asp:Label>
- <br /><br />
- <asp:Button
- ID="Button1"
- runat="server"
- Text="string replace"
- OnClick="Button1_Click"
- Height="40"
- Font-Bold="true"
- />
- </div>
- </form>
- </body>
- </html>


- <%@ Page Language="C#" AutoEventWireup="true"%>
- <!DOCTYPE html>
- <script runat="server">
- protected void Button1_Click(object sender, System.EventArgs e)
- {
- //this line create a string variable.
- string stringValue = "red";
- string stringValue2 = "green";
- string stringValue3 = "yellow";
- string stringValue4 = "crimson";
- string stringValue5 = "indianred";
- Label1.Text = "string value: " + stringValue.ToString();
- Label1.Text += "<br />string value2: " + stringValue2.ToString();
- Label1.Text += "<br />string value3: " + stringValue3.ToString();
- Label1.Text += "<br />string value4: " + stringValue4.ToString();
- Label1.Text += "<br />string value5: " + stringValue5.ToString();
- Label1.Text += "<br /><br />formatted string<br /><br />";
- //make string fixed length 25
- Label1.Text += stringValue.PadLeft(25, '-');
- Label1.Text += "<br />";
- Label1.Text += stringValue2.PadLeft(25, '-');
- Label1.Text += "<br />";
- Label1.Text += stringValue3.PadLeft(25, '-');
- Label1.Text += "<br />";
- Label1.Text += stringValue4.PadLeft(25, '-');
- Label1.Text += "<br />";
- Label1.Text += stringValue5.PadLeft(25, '-');
- //another technique to format string fixed length
- //String.Format("color = |{0,25}|", stringValue);
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>c# example - string format fixed length</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:MidnightBlue; font-style:italic;">
- c# example - string format fixed length
- </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 format fixed length"
- OnClick="Button1_Click"
- Height="40"
- Font-Bold="true"
- />
- </div>
- </form>
- </body>
- </html>


- <%@ Page Language="C#" AutoEventWireup="true"%>
- <!DOCTYPE html>
- <script runat="server">
- protected void Button1_Click(object sender, System.EventArgs e)
- {
- //this line create a string variable.
- string stringValue = "asp.net examples";
- //this line create an integer variable.
- int intValue = 12345;
- Label1.Text = "string value: " + stringValue.ToString();
- Label1.Text += "<br />integer value: " + intValue.ToString();
- //string format left padding and right padding
- Label1.Text += "<br /><br />string format left padding with 0: " + stringValue.ToString().PadLeft(stringValue.Length + 1, '0');
- Label1.Text += "<br />string format left padding with 3 zeros: " + stringValue.ToString().PadLeft(stringValue.Length + 3, '0');
- Label1.Text += "<br /><br />string format number to leading 2 zeros: " + intValue.ToString().PadLeft(intValue.ToString().Length + 2, '0');
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>c# example - string format leading zeros</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:MidnightBlue; font-style:italic;">
- c# example - string format leading zeros
- </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 format leading zeros"
- OnClick="Button1_Click"
- Height="40"
- Font-Bold="true"
- />
- </div>
- </form>
- </body>
- </html>


- <%@ Page Language="C#" AutoEventWireup="true"%>
- <!DOCTYPE html>
- <script runat="server">
- protected void Button1_Click(object sender, System.EventArgs e)
- {
- //this line create a string variable.
- string stringValue = "c# examples";
- Label1.Text = "string value: " + stringValue.ToString();
- //string format left padding and right padding
- Label1.Text += "<br /><br />string format with left padding: " + stringValue.ToString().PadLeft(stringValue.Length + 1, '@');
- Label1.Text += "<br />string format with right padding: " + stringValue.ToString().PadRight(stringValue.Length + 1, '@');
- Label1.Text += "<br /><br />string format with left padding: " + stringValue.ToString().PadLeft(stringValue.Length + 2, '@');
- Label1.Text += "<br />string format with right padding: " + stringValue.ToString().PadRight(stringValue.Length + 2, '@');
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>c# example - string format padding</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:MidnightBlue; font-style:italic;">
- c# example - string format padding
- </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 format padding"
- OnClick="Button1_Click"
- Height="40"
- Font-Bold="true"
- />
- </div>
- </form>
- </body>
- </html>


- <%@ Page Language="C#" AutoEventWireup="true"%>
- <!DOCTYPE html>
- <script runat="server">
- protected void Button1_Click(object sender, System.EventArgs e)
- {
- //this line create an integer variable.
- int integerValue = 183455;
- Label1.Text = "integer value: " + integerValue.ToString();
- //string format number to fixed length hexadecimal format using string.format method.
- Label1.Text += "<br /><br />number in hexadecimal format: " + string.Format("{0:x}", integerValue);
- Label1.Text += "<br />hexadecimal format(0:x6) padding length 6: " + string.Format("{0:x6}", integerValue);
- Label1.Text += "<br />hexadecimal format fixed length 8: " + string.Format("{0:x8}", integerValue);
- Label1.Text += "<br />hexadecimal format pad length 10: " + string.Format("{0:x10}", integerValue);
- Label1.Text += "<br /><br />using ToString method fixed length 6: " + integerValue.ToString("x6");
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>c# example - string format hex padding</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:MidnightBlue; font-style:italic;">
- c# example - string format hex padding
- </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 format hex padding"
- OnClick="Button1_Click"
- Height="40"
- Font-Bold="true"
- />
- </div>
- </form>
- </body>
- </html>


- <%@ Page Language="C#" AutoEventWireup="true"%>
- <!DOCTYPE html>
- <script runat="server">
- protected void Button1_Click(object sender, System.EventArgs e)
- {
- //this line create an integer variable.
- int intValue = 123456;
- Label1.Text = "integer value: " + intValue.ToString();
- //string format number to hexadecimal format using string.format method.
- Label1.Text += "<br /><br />number in hexadecimal format: " + string.Format("{0:x}", intValue);
- Label1.Text += "<br />number in hexadecimal format(0:x6): " + string.Format("{0:x6}", intValue);
- Label1.Text += "<br />number in hexadecimal format(0:x8): " + string.Format("{0:x8}", intValue);
- Label1.Text += "<br /><br />using ToString method: " + intValue.ToString("x");
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>c# example - string format hexadecimal</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:MidnightBlue; font-style:italic;">
- c# example - string format hexadecimal
- </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 format hexadecimal"
- OnClick="Button1_Click"
- Height="40"
- Font-Bold="true"
- />
- </div>
- </form>
- </body>
- </html>


- <%@ Page Language="C#" AutoEventWireup="true"%>
- <!DOCTYPE html>
- <script runat="server">
- protected void Button1_Click(object sender, System.EventArgs e)
- {
- //this line create a decimal variable.
- Decimal decimalValue1 = 0.5567M;
- Label1.Text = "decimal value1: " + decimalValue1.ToString();
- //string format decimal to int using string.format method.
- Label1.Text += "<br /><br />decimal value1 percent format: " + string.Format("{0:p}", decimalValue1);
- Label1.Text += "<br />percent Format with 0 decimal digits: " + string.Format("{0:p0}", decimalValue1);
- Label1.Text += "<br />percent Format with 1 decimal digits: " + string.Format("{0:p1}", decimalValue1);
- Label1.Text += "<br />percent Format with 2 decimal digits: " + string.Format("{0:p2}", decimalValue1);
- Label1.Text += "<br />percent Format with 3 decimal digits: " + string.Format("{0:p3}", decimalValue1);
- Label1.Text += "<br /><br />using ToString method: " + decimalValue1.ToString("p1");
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>c# example - string format decimal percentage</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:MidnightBlue; font-style:italic;">
- c# example - string format decimal percentage
- </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 format decimal percentage"
- OnClick="Button1_Click"
- Height="40"
- Font-Bold="true"
- />
- </div>
- </form>
- </body>
- </html>


- <%@ Page Language="C#" AutoEventWireup="true"%>
- <!DOCTYPE html>
- <script runat="server">
- protected void Button1_Click(object sender, System.EventArgs e)
- {
- //this section create decimal variables.
- Decimal decimalValue1 = 123.55M;
- Decimal decimalValue2 = 555.45M;
- Label1.Text = "decimal value1: " + decimalValue1.ToString();
- Label1.Text += "<br />decimal value2: " + decimalValue2.ToString();
- //string format decimal to int using string.format method.
- Label1.Text += "<br /><br />decimal value1 to int: " + string.Format("{0:0}", decimalValue2);
- Label1.Text += "<br />decimal value2 to int: " + string.Format("{0:0}", decimalValue2);
- Label1.Text += "<br /><br />decimal value1 (Math.Floor): " + string.Format("{0:0}", Math.Floor(decimalValue1));
- Label1.Text += "<br />decimal value1 (Math.Round): " + string.Format("{0:0}", Math.Round(decimalValue1));
- Label1.Text += "<br /><br />decimal value2 (Math.Floor): " + string.Format("{0:0}", Math.Floor(decimalValue2));
- Label1.Text += "<br />decimal value2 (Math.Round): " + string.Format("{0:0}", Math.Round(decimalValue2));
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>c# example - string format decimal to int</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:MidnightBlue; font-style:italic;">
- c# example - string format decimal to int
- </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 format decimal to int"
- OnClick="Button1_Click"
- Height="40"
- Font-Bold="true"
- />
- </div>
- </form>
- </body>
- </html>


No comments:
Post a Comment