Wednesday, 23 October 2013

ASPDOTNET C# STRING MANIPULATION EXAMPLES - MANOHAR GOUD 9866550012

ASPDOTNET C# STRING MANIPULATION EXAMPLES

  1. <%@ Page Language="C#" AutoEventWireup="true"%>  
  2.   
  3. <!DOCTYPE html>    
  4. <script runat="server">   
  5.     protected void Button1_Click(object sender, System.EventArgs e)    
  6.     {  
  7.         //this section create a string variable.  
  8.         string stringValue = "red green Red yellow darkRed";  
  9.   
  10.         Label1.Text = "string..................<br />";  
  11.         Label1.Text += stringValue.ToString();  
  12.   
  13.         //this section replace substring with substring in string ignore case.  
  14.         string replacedString = Regex.Replace(stringValue, "red", "blue", RegexOptions.IgnoreCase);  
  15.   
  16.         Label1.Text += "<br /><br />modified string......................";  
  17.         Label1.Text += "<br />" + replacedString.ToString();  
  18.     }    
  19. </script>    
  20.     
  21. <html xmlns="http://www.w3.org/1999/xhtml">    
  22. <head id="Head1" runat="server">    
  23.     <title>c# example - string replace ignore case</title>    
  24. </head>    
  25. <body>    
  26.     <form id="form1" runat="server">    
  27.     <div>    
  28.         <h2 style="color:MidnightBlue; font-style:italic;">    
  29.             c# example - string replace ignore case  
  30.         </h2>    
  31.         <hr width="550" align="left" color="Gainsboro" />    
  32.         <asp:Label     
  33.             ID="Label1"     
  34.             runat="server"    
  35.             Font-Size="Large"  
  36.             Font-Bold="true"  
  37.             >    
  38.         </asp:Label>    
  39.         <br /><br />  
  40.         <asp:Button     
  41.             ID="Button1"     
  42.             runat="server"     
  43.             Text="string replace ignore case"    
  44.             OnClick="Button1_Click"  
  45.             Height="40"    
  46.             Font-Bold="true"    
  47.             />    
  48.     </div>    
  49.     </form>    
  50. </body>    
  51. </html>  






  1. <%@ Page Language="C#" AutoEventWireup="true"%>  
  2.   
  3. <!DOCTYPE html>    
  4. <script runat="server">   
  5.     protected void Button1_Click(object sender, System.EventArgs e)    
  6.     {  
  7.         //this section create string variables.  
  8.         string stringValue = "1 2 3 4 5 6 7 8 9 10";  
  9.         string stringValue2 = "replacing multiple characters in a string.";  
  10.   
  11.         Label1.Text = "string1: " + stringValue.ToString();  
  12.         Label1.Text += "<br />string2: " + stringValue2.ToString();  
  13.   
  14.         //this section replace charcters with character in string.  
  15.         string modifiedString1 = stringValue.Replace(' ', '-');  
  16.         string modifiedString2 = stringValue2.Replace('a', 'k');  
  17.   
  18.         Label1.Text += "<br /><br />modified string 1......................";  
  19.         Label1.Text += "<br />" + modifiedString1.ToString();  
  20.   
  21.         Label1.Text += "<br /><br />modified string 2......................";  
  22.         Label1.Text += "<br />" + modifiedString2.ToString();  
  23.     }    
  24. </script>    
  25.     
  26. <html xmlns="http://www.w3.org/1999/xhtml">    
  27. <head id="Head1" runat="server">    
  28.     <title>c# example - string replace character</title>    
  29. </head>    
  30. <body>    
  31.     <form id="form1" runat="server">    
  32.     <div>    
  33.         <h2 style="color:MidnightBlue; font-style:italic;">    
  34.             c# example - string replace character  
  35.         </h2>    
  36.         <hr width="550" align="left" color="Gainsboro" />    
  37.         <asp:Label     
  38.             ID="Label1"     
  39.             runat="server"    
  40.             Font-Size="Large"  
  41.             Font-Bold="true"  
  42.             >    
  43.         </asp:Label>    
  44.         <br /><br />  
  45.         <asp:Button     
  46.             ID="Button1"     
  47.             runat="server"     
  48.             Text="string replace character"    
  49.             OnClick="Button1_Click"  
  50.             Height="40"    
  51.             Font-Bold="true"    
  52.             />    
  53.     </div>    
  54.     </form>    
  55. </body>    
  56. </html>  





  1. <%@ Page Language="C#" AutoEventWireup="true"%>  
  2.   
  3. <!DOCTYPE html>    
  4. <script runat="server">   
  5.     protected void Button1_Click(object sender, System.EventArgs e)    
  6.     {  
  7.         //this line create a string variable.  
  8.         string stringValue = "how is it possible to replace a string in a string?";  
  9.   
  10.         Label1.Text = "string......................";  
  11.         Label1.Text += "<br />string: " + stringValue.ToString();  
  12.   
  13.         string replaceWith = "impossible";  
  14.         //this line replace 'possible' substring with 'impossible' in string.  
  15.         string modifiedString = stringValue.Replace("possible", replaceWith);  
  16.   
  17.         Label1.Text += "<br /><br />modified string......................";  
  18.         Label1.Text += "<br />" + modifiedString.ToString();  
  19.     }    
  20. </script>    
  21.     
  22. <html xmlns="http://www.w3.org/1999/xhtml">    
  23. <head id="Head1" runat="server">    
  24.     <title>c# example - string replace</title>    
  25. </head>    
  26. <body>    
  27.     <form id="form1" runat="server">    
  28.     <div>    
  29.         <h2 style="color:MidnightBlue; font-style:italic;">    
  30.             c# example - string replace  
  31.         </h2>    
  32.         <hr width="550" align="left" color="Gainsboro" />    
  33.         <asp:Label     
  34.             ID="Label1"     
  35.             runat="server"    
  36.             Font-Size="Large"  
  37.             Font-Bold="true"  
  38.             >    
  39.         </asp:Label>    
  40.         <br /><br />  
  41.         <asp:Button     
  42.             ID="Button1"     
  43.             runat="server"     
  44.             Text="string replace"    
  45.             OnClick="Button1_Click"  
  46.             Height="40"    
  47.             Font-Bold="true"    
  48.             />    
  49.     </div>    
  50.     </form>    
  51. </body>    
  52. </html>  





  1. <%@ Page Language="C#" AutoEventWireup="true"%>  
  2.   
  3. <!DOCTYPE html>    
  4. <script runat="server">   
  5.     protected void Button1_Click(object sender, System.EventArgs e)    
  6.     {  
  7.         //this line create a string variable.  
  8.         string stringValue = "red";  
  9.         string stringValue2 = "green";  
  10.         string stringValue3 = "yellow";  
  11.         string stringValue4 = "crimson";  
  12.         string stringValue5 = "indianred";  
  13.   
  14.         Label1.Text = "string value: " + stringValue.ToString();  
  15.         Label1.Text += "<br />string value2: " + stringValue2.ToString();  
  16.         Label1.Text += "<br />string value3: " + stringValue3.ToString();  
  17.         Label1.Text += "<br />string value4: " + stringValue4.ToString();  
  18.         Label1.Text += "<br />string value5: " + stringValue5.ToString();  
  19.   
  20.         Label1.Text += "<br /><br />formatted string<br /><br />";  
  21.   
  22.         //make string fixed length 25  
  23.         Label1.Text += stringValue.PadLeft(25, '-');  
  24.         Label1.Text += "<br />";  
  25.         Label1.Text += stringValue2.PadLeft(25, '-');  
  26.         Label1.Text += "<br />";  
  27.         Label1.Text += stringValue3.PadLeft(25, '-');  
  28.         Label1.Text += "<br />";  
  29.         Label1.Text += stringValue4.PadLeft(25, '-');  
  30.         Label1.Text += "<br />";  
  31.         Label1.Text += stringValue5.PadLeft(25, '-');  
  32.   
  33.         //another technique to format string fixed length  
  34.         //String.Format("color = |{0,25}|", stringValue);  
  35.     }    
  36. </script>    
  37.     
  38. <html xmlns="http://www.w3.org/1999/xhtml">    
  39. <head id="Head1" runat="server">    
  40.     <title>c# example - string format fixed length</title>    
  41. </head>    
  42. <body>    
  43.     <form id="form1" runat="server">    
  44.     <div>    
  45.         <h2 style="color:MidnightBlue; font-style:italic;">    
  46.             c# example - string format fixed length  
  47.         </h2>    
  48.         <hr width="550" align="left" color="Gainsboro" />    
  49.         <asp:Label     
  50.             ID="Label1"     
  51.             runat="server"    
  52.             Font-Size="Large"    
  53.             >    
  54.         </asp:Label>    
  55.         <br /><br />  
  56.         <asp:Button     
  57.             ID="Button1"     
  58.             runat="server"     
  59.             Text="string format fixed length"    
  60.             OnClick="Button1_Click"  
  61.             Height="40"    
  62.             Font-Bold="true"    
  63.             />    
  64.     </div>    
  65.     </form>    
  66. </body>    
  67. </html>  





  1. <%@ Page Language="C#" AutoEventWireup="true"%>  
  2.   
  3. <!DOCTYPE html>    
  4. <script runat="server">   
  5.     protected void Button1_Click(object sender, System.EventArgs e)    
  6.     {  
  7.         //this line create a string variable.  
  8.         string stringValue = "asp.net examples";  
  9.   
  10.         //this line create an integer variable.  
  11.         int intValue = 12345;  
  12.   
  13.         Label1.Text = "string value: " + stringValue.ToString();  
  14.         Label1.Text += "<br />integer value: " + intValue.ToString();  
  15.   
  16.         //string format left padding and right padding  
  17.         Label1.Text += "<br /><br />string format left padding with 0: " + stringValue.ToString().PadLeft(stringValue.Length + 1, '0');  
  18.         Label1.Text += "<br />string format left padding with 3 zeros: " + stringValue.ToString().PadLeft(stringValue.Length + 3, '0');  
  19.   
  20.         Label1.Text += "<br /><br />string format number to leading 2 zeros: " + intValue.ToString().PadLeft(intValue.ToString().Length + 2, '0');  
  21.     }    
  22. </script>    
  23.     
  24. <html xmlns="http://www.w3.org/1999/xhtml">    
  25. <head id="Head1" runat="server">    
  26.     <title>c# example - string format leading zeros</title>    
  27. </head>    
  28. <body>    
  29.     <form id="form1" runat="server">    
  30.     <div>    
  31.         <h2 style="color:MidnightBlue; font-style:italic;">    
  32.             c# example - string format leading zeros  
  33.         </h2>    
  34.         <hr width="550" align="left" color="Gainsboro" />    
  35.         <asp:Label     
  36.             ID="Label1"     
  37.             runat="server"    
  38.             Font-Size="Large"    
  39.             >    
  40.         </asp:Label>    
  41.         <br /><br />  
  42.         <asp:Button     
  43.             ID="Button1"     
  44.             runat="server"     
  45.             Text="string format leading zeros"    
  46.             OnClick="Button1_Click"  
  47.             Height="40"    
  48.             Font-Bold="true"    
  49.             />    
  50.     </div>    
  51.     </form>    
  52. </body>    
  53. </html>  




  1. <%@ Page Language="C#" AutoEventWireup="true"%>  
  2.   
  3. <!DOCTYPE html>    
  4. <script runat="server">   
  5.     protected void Button1_Click(object sender, System.EventArgs e)    
  6.     {  
  7.         //this line create a string variable.  
  8.         string stringValue = "c# examples";  
  9.   
  10.         Label1.Text = "string value: " + stringValue.ToString();  
  11.   
  12.         //string format left padding and right padding  
  13.         Label1.Text += "<br /><br />string format with left padding: " + stringValue.ToString().PadLeft(stringValue.Length + 1, '@');  
  14.         Label1.Text += "<br />string format with right padding: " + stringValue.ToString().PadRight(stringValue.Length + 1, '@');  
  15.   
  16.         Label1.Text += "<br /><br />string format with left padding: " + stringValue.ToString().PadLeft(stringValue.Length + 2, '@');  
  17.         Label1.Text += "<br />string format with right padding: " + stringValue.ToString().PadRight(stringValue.Length + 2, '@');  
  18.     }    
  19. </script>    
  20.     
  21. <html xmlns="http://www.w3.org/1999/xhtml">    
  22. <head id="Head1" runat="server">    
  23.     <title>c# example - string format padding</title>    
  24. </head>    
  25. <body>    
  26.     <form id="form1" runat="server">    
  27.     <div>    
  28.         <h2 style="color:MidnightBlue; font-style:italic;">    
  29.             c# example - string format padding  
  30.         </h2>    
  31.         <hr width="550" align="left" color="Gainsboro" />    
  32.         <asp:Label     
  33.             ID="Label1"     
  34.             runat="server"    
  35.             Font-Size="Large"    
  36.             >    
  37.         </asp:Label>    
  38.         <br /><br />  
  39.         <asp:Button     
  40.             ID="Button1"     
  41.             runat="server"     
  42.             Text="string format padding"    
  43.             OnClick="Button1_Click"  
  44.             Height="40"    
  45.             Font-Bold="true"    
  46.             />    
  47.     </div>    
  48.     </form>    
  49. </body>    
  50. </html>  




  1. <%@ Page Language="C#" AutoEventWireup="true"%>  
  2.   
  3. <!DOCTYPE html>    
  4. <script runat="server">   
  5.     protected void Button1_Click(object sender, System.EventArgs e)    
  6.     {  
  7.         //this line create an integer variable.  
  8.         int integerValue = 183455;  
  9.   
  10.         Label1.Text = "integer value: " + integerValue.ToString();  
  11.   
  12.         //string format number to fixed length hexadecimal format using string.format method.  
  13.         Label1.Text += "<br /><br />number in hexadecimal format: " + string.Format("{0:x}", integerValue);  
  14.         Label1.Text += "<br />hexadecimal format(0:x6) padding length 6: " + string.Format("{0:x6}", integerValue);  
  15.         Label1.Text += "<br />hexadecimal format fixed length 8: " + string.Format("{0:x8}", integerValue);  
  16.         Label1.Text += "<br />hexadecimal format pad length 10: " + string.Format("{0:x10}", integerValue);  
  17.   
  18.         Label1.Text += "<br /><br />using ToString method fixed length 6: " + integerValue.ToString("x6");  
  19.     }    
  20. </script>    
  21.     
  22. <html xmlns="http://www.w3.org/1999/xhtml">    
  23. <head id="Head1" runat="server">    
  24.     <title>c# example - string format hex padding</title>    
  25. </head>    
  26. <body>    
  27.     <form id="form1" runat="server">    
  28.     <div>    
  29.         <h2 style="color:MidnightBlue; font-style:italic;">    
  30.             c# example - string format hex padding  
  31.         </h2>    
  32.         <hr width="550" align="left" color="Gainsboro" />    
  33.         <asp:Label     
  34.             ID="Label1"     
  35.             runat="server"    
  36.             Font-Size="Large"    
  37.             >    
  38.         </asp:Label>    
  39.         <br /><br />  
  40.         <asp:Button     
  41.             ID="Button1"     
  42.             runat="server"     
  43.             Text="string format hex padding"    
  44.             OnClick="Button1_Click"  
  45.             Height="40"    
  46.             Font-Bold="true"    
  47.             />    
  48.     </div>    
  49.     </form>    
  50. </body>    
  51. </html>  





  1. <%@ Page Language="C#" AutoEventWireup="true"%>  
  2.   
  3. <!DOCTYPE html>    
  4. <script runat="server">   
  5.     protected void Button1_Click(object sender, System.EventArgs e)    
  6.     {  
  7.         //this line create an integer variable.  
  8.         int intValue = 123456;  
  9.   
  10.         Label1.Text = "integer value: " + intValue.ToString();  
  11.   
  12.         //string format number to hexadecimal format using string.format method.  
  13.         Label1.Text += "<br /><br />number in hexadecimal format: " + string.Format("{0:x}", intValue);  
  14.         Label1.Text += "<br />number in hexadecimal format(0:x6): " + string.Format("{0:x6}", intValue);  
  15.         Label1.Text += "<br />number in hexadecimal format(0:x8): " + string.Format("{0:x8}", intValue);  
  16.   
  17.         Label1.Text += "<br /><br />using ToString method: " + intValue.ToString("x");  
  18.     }    
  19. </script>    
  20.     
  21. <html xmlns="http://www.w3.org/1999/xhtml">    
  22. <head id="Head1" runat="server">    
  23.     <title>c# example - string format hexadecimal</title>    
  24. </head>    
  25. <body>    
  26.     <form id="form1" runat="server">    
  27.     <div>    
  28.         <h2 style="color:MidnightBlue; font-style:italic;">    
  29.             c# example - string format hexadecimal  
  30.         </h2>    
  31.         <hr width="550" align="left" color="Gainsboro" />    
  32.         <asp:Label     
  33.             ID="Label1"     
  34.             runat="server"    
  35.             Font-Size="Large"    
  36.             >    
  37.         </asp:Label>    
  38.         <br /><br />  
  39.         <asp:Button     
  40.             ID="Button1"     
  41.             runat="server"     
  42.             Text="string format hexadecimal"    
  43.             OnClick="Button1_Click"  
  44.             Height="40"    
  45.             Font-Bold="true"    
  46.             />    
  47.     </div>    
  48.     </form>    
  49. </body>    
  50. </html>  




  1. <%@ Page Language="C#" AutoEventWireup="true"%>  
  2.   
  3. <!DOCTYPE html>    
  4. <script runat="server">   
  5.     protected void Button1_Click(object sender, System.EventArgs e)    
  6.     {  
  7.         //this line create a decimal variable.  
  8.         Decimal decimalValue1 = 0.5567M;  
  9.   
  10.         Label1.Text = "decimal value1: " + decimalValue1.ToString();  
  11.   
  12.         //string format decimal to int using string.format method.  
  13.         Label1.Text += "<br /><br />decimal value1 percent format: " + string.Format("{0:p}", decimalValue1);  
  14.         Label1.Text += "<br />percent Format with 0 decimal digits: " + string.Format("{0:p0}", decimalValue1);  
  15.         Label1.Text += "<br />percent Format with 1 decimal digits: " + string.Format("{0:p1}", decimalValue1);  
  16.         Label1.Text += "<br />percent Format with 2 decimal digits: " + string.Format("{0:p2}", decimalValue1);  
  17.         Label1.Text += "<br />percent Format with 3 decimal digits: " + string.Format("{0:p3}", decimalValue1);  
  18.   
  19.         Label1.Text += "<br /><br />using ToString method: " + decimalValue1.ToString("p1");  
  20.     }    
  21. </script>    
  22.     
  23. <html xmlns="http://www.w3.org/1999/xhtml">    
  24. <head id="Head1" runat="server">    
  25.     <title>c# example - string format decimal percentage</title>    
  26. </head>    
  27. <body>    
  28.     <form id="form1" runat="server">    
  29.     <div>    
  30.         <h2 style="color:MidnightBlue; font-style:italic;">    
  31.             c# example - string format decimal percentage  
  32.         </h2>    
  33.         <hr width="550" align="left" color="Gainsboro" />    
  34.         <asp:Label     
  35.             ID="Label1"     
  36.             runat="server"    
  37.             Font-Size="Large"    
  38.             >    
  39.         </asp:Label>    
  40.         <br /><br />  
  41.         <asp:Button     
  42.             ID="Button1"     
  43.             runat="server"     
  44.             Text="string format decimal percentage"    
  45.             OnClick="Button1_Click"  
  46.             Height="40"    
  47.             Font-Bold="true"    
  48.             />    
  49.     </div>    
  50.     </form>    
  51. </body>    
  52. </html>  






  1. <%@ Page Language="C#" AutoEventWireup="true"%>  
  2.   
  3. <!DOCTYPE html>    
  4. <script runat="server">   
  5.     protected void Button1_Click(object sender, System.EventArgs e)    
  6.     {  
  7.         //this section create decimal variables.  
  8.         Decimal decimalValue1 = 123.55M;  
  9.         Decimal decimalValue2 = 555.45M;  
  10.   
  11.         Label1.Text = "decimal value1: " + decimalValue1.ToString();  
  12.         Label1.Text += "<br />decimal value2: " + decimalValue2.ToString();  
  13.   
  14.         //string format decimal to int using string.format method.  
  15.         Label1.Text += "<br /><br />decimal value1 to int: " + string.Format("{0:0}", decimalValue2);  
  16.         Label1.Text += "<br />decimal value2 to int: " + string.Format("{0:0}", decimalValue2);  
  17.   
  18.         Label1.Text += "<br /><br />decimal value1 (Math.Floor): " + string.Format("{0:0}", Math.Floor(decimalValue1));  
  19.         Label1.Text += "<br />decimal value1 (Math.Round): " + string.Format("{0:0}", Math.Round(decimalValue1));  
  20.   
  21.         Label1.Text += "<br /><br />decimal value2 (Math.Floor): " + string.Format("{0:0}", Math.Floor(decimalValue2));  
  22.         Label1.Text += "<br />decimal value2 (Math.Round): " + string.Format("{0:0}", Math.Round(decimalValue2));  
  23.     }    
  24. </script>    
  25.     
  26. <html xmlns="http://www.w3.org/1999/xhtml">    
  27. <head id="Head1" runat="server">    
  28.     <title>c# example - string format decimal to int</title>    
  29. </head>    
  30. <body>    
  31.     <form id="form1" runat="server">    
  32.     <div>    
  33.         <h2 style="color:MidnightBlue; font-style:italic;">    
  34.             c# example - string format decimal to int  
  35.         </h2>    
  36.         <hr width="550" align="left" color="Gainsboro" />    
  37.         <asp:Label     
  38.             ID="Label1"     
  39.             runat="server"    
  40.             Font-Size="Large"    
  41.             >    
  42.         </asp:Label>    
  43.         <br /><br />  
  44.         <asp:Button     
  45.             ID="Button1"     
  46.             runat="server"     
  47.             Text="string format decimal to int"    
  48.             OnClick="Button1_Click"  
  49.             Height="40"    
  50.             Font-Bold="true"    
  51.             />    
  52.     </div>    
  53.     </form>    
  54. </body>    
  55. </html>