简体   繁体   中英

how to split the characters in a string and perform a sum in C#

I am having a sting with value in it as "12345" what i have to do is i have to perform a split operation and add that using for loops like: 1+2+3+4+5 and then display the output of the operation in a label as 15.

i am posting my code here in asp.net and c#

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace proj1 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { int v1 = 0, x = 0; //sum = 0, l, r; // char Q, W, E, R, T, Y, U, I, O, P, A, S, D, F, G, H, J, K, L, Z, X, C, V, B, N, M; string str = TextBox1.Text; Label1.Text = ("Result"); Label2.Text = ("You have Entered"); Label5.Text = (":"); Label8.Text = str.ToString(); // Array word[] = new Array(); string str1 = str.Replace(" ", "").Replace("`", "").Replace("~", "").Replace("!", "") .Replace("@", "").Replace("#", "").Replace("$", "").Replace("%", "").Replace("^", "") .Replace("&", "").Replace("*", "").Replace("(", "").Replace(")", "").Replace("-", "") .Replace("_", "").Replace("=", "").Replace("+", "").Replace("{", "").Replace("}", "") .Replace("|", "").Replace("[", "").Replace("]", "").Replace(":", "").Replace(";", "") .Replace("'", "").Replace(",", "").Replace(".", "").Replace("/", "").Replace("?", "") .Replace(">", "").Replace("<", "").Replace("1", "").Replace("2", "").Replace("3", "") .Replace("4", "").Replace("5", "").Replace("6", "").Replace("7", "").Replace("8", "") .Replace("9", "").Replace("0", ""); string str2 = str1.ToUpper(); string str3 = str2.Replace("A", "1").Replace("B", "2").Replace("C", "3").Replace("D", "4").Replace("E", "5").Replace("F", "8") .Replace("G", "3").Replace("H", "5").Replace("I", "1").Replace("J", "1").Replace("K", "2").Replace("L", "3") .Replace("M", "4").Replace("N", "5").Replace("O", "7").Replace("P", "8").Replace("Q", "1").Replace("R", "2") .Replace("S", "3").Replace("T", "4").Replace("U", "6").Replace("V", "6").Replace("W", "6").Replace("X", "5") .Replace("Y", "1").Replace("Z", "7"); string word1 = str3.ToString(); String[] word = word1.ToString().Split(); for (int i = 0; i < word.Length; i++) { x = (( x + (Convert.ToInt32(word[i])))); } int value1 = x; Label3.Text = ("Value Of Charecters"); Label6.Text = (":"); Label9.Text = value1.ToString(); Label4.Text = ("Summed Value"); Label7.Text = (":"); Label10.Text = (""); } } } 
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="proj1.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <h1>Project - 1</h1> <form id="form1" runat="server"> <div> <table> <tr> <td class="auto-style1">Enter Your Text</td> <td>:</td> <td> <asp:TextBox ID="TextBox1" runat="server" Width="362px"></asp:TextBox> </td> </tr> <tr> <td class="auto-style1"></td> <td></td> <td> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /> </td> </tr> <tr> <td class="auto-style1"></td> <td></td> <td></td> </tr> <tr> <td class="auto-style1"> <asp:Label ID="Label1" runat="server"></asp:Label> </td> <td></td> <td></td> </tr> <tr> <td class="auto-style1"></td> <td></td> <td></td> </tr> <tr> <td class="auto-style1"> <asp:Label ID="Label2" runat="server"></asp:Label> </td> <td> <asp:Label ID="Label5" runat="server"></asp:Label> </td> <td> <asp:Label ID="Label8" runat="server"></asp:Label> </td> </tr> <tr> <td class="auto-style1"> <asp:Label ID="Label3" runat="server"></asp:Label> </td> <td> <asp:Label ID="Label6" runat="server"></asp:Label> </td> <td> <asp:Label ID="Label9" runat="server"></asp:Label> </td> </tr> <tr> <td class="auto-style1"> <asp:Label ID="Label4" runat="server"></asp:Label> </td> <td> <asp:Label ID="Label7" runat="server"></asp:Label> </td> <td> <asp:Label ID="Label10" runat="server"></asp:Label> </td> </tr> </table> </div> </form> </body> </html> 

Actually i have to perform this task

  1. Input Characters in text box. then on button click
  2. Save the characters and display the input
  3. replace the characters with the given numbers in excel sheet.
  4. Perform the sum like if a=1,b=2,c=3,d=4,e=5 then you should display 15 as output as the result of that sum after that.
  5. Separate the values in the sum say 15 as 1 and 5 which are inputs for another function for totalling until the final summation should be single digit
  6. Generate exe file

I suggest using Linq :

  using System.Linq;

  ...

  string source = "12345";

  myLabel.Text = source
    .Select(c => c - '0') // character to integer '0' -> 0 .. '9' -> 9  
    .Sum()                // sum up
    .ToString();          // represent as string

Here the script is not Java script it is C# code

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace proj1 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string str = TextBox1.Text; Label1.Text = ("Result"); Label2.Text = ("You have Entered"); Label5.Text = (":"); Label8.Text = str.ToString(); string str1 = str.Replace(" ", "").Replace("`", "").Replace("~", "").Replace("!", "") .Replace("@", "").Replace("#", "").Replace("$", "").Replace("%", "").Replace("^", "") .Replace("&", "").Replace("*", "").Replace("(", "").Replace(")", "").Replace("-", "") .Replace("_", "").Replace("=", "").Replace("+", "").Replace("{", "").Replace("}", "") .Replace("|", "").Replace("[", "").Replace("]", "").Replace(":", "").Replace(";", "") .Replace("'", "").Replace(",", "").Replace(".", "").Replace("/", "").Replace("?", "") .Replace(">", "").Replace("<", "").Replace("1", "").Replace("2", "").Replace("3", "") .Replace("4", "").Replace("5", "").Replace("6", "").Replace("7", "").Replace("8", "") .Replace("9", "").Replace("0", ""); string str2 = str1.ToUpper(); string str3 = str2.Replace("A", "1").Replace("B", "2").Replace("C", "3").Replace("D", "4").Replace("E", "5").Replace("F", "8") .Replace("G", "3").Replace("H", "5").Replace("I", "1").Replace("J", "1").Replace("K", "2").Replace("L", "3") .Replace("M", "4").Replace("N", "5").Replace("O", "7").Replace("P", "8").Replace("Q", "1").Replace("R", "2") .Replace("S", "3").Replace("T", "4").Replace("U", "6").Replace("V", "6").Replace("W", "6").Replace("X", "5") .Replace("Y", "1").Replace("Z", "7"); string word1 = str3.ToString(); string a = word1.Select(c => c - '0').Sum().ToString(); Label3.Text = ("Sum Of Charecters"); Label6.Text = (":"); Label9.Text = a.ToString(); string w = Label9.ToString(); string s = w.ToString(); string b = a.Select(c => c - '0').Sum().ToString(); for (int i = 0; i < b.Length; i++) { string p = b.Select(c => c - '0').Sum().ToString(); Label10.Text = p.ToString(); } Label4.Text = ("Summed Value"); Label7.Text = (":"); } } } 
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="proj1.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <h1>Project - 1</h1> <form id="form1" runat="server"> <div> <table> <tr> <td class="auto-style1">Enter Your Text</td> <td>:</td> <td> <asp:TextBox ID="TextBox1" runat="server" Width="362px"></asp:TextBox> </td> </tr> <tr> <td class="auto-style1"></td> <td></td> <td> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" /> </td> </tr> <tr> <td class="auto-style1"></td> <td></td> <td></td> </tr> <tr> <td class="auto-style1"> <asp:Label ID="Label1" runat="server"></asp:Label> </td> <td></td> <td></td> </tr> <tr> <td class="auto-style1"></td> <td></td> <td></td> </tr> <tr> <td class="auto-style1"> <asp:Label ID="Label2" runat="server"></asp:Label> </td> <td> <asp:Label ID="Label5" runat="server"></asp:Label> </td> <td> <asp:Label ID="Label8" runat="server"></asp:Label> </td> </tr> <tr> <td class="auto-style1"> <asp:Label ID="Label3" runat="server"></asp:Label> </td> <td> <asp:Label ID="Label6" runat="server"></asp:Label> </td> <td> <asp:Label ID="Label9" runat="server"></asp:Label> </td> </tr> <tr> <td class="auto-style1"> <asp:Label ID="Label4" runat="server"></asp:Label> </td> <td> <asp:Label ID="Label7" runat="server"></asp:Label> </td> <td> <asp:Label ID="Label10" runat="server"></asp:Label> </td> </tr> </table> </div> </form> </body> </html> 

You can try:

string source = "12345";
var sum = 0;
foreach (var element in source)
  sum += Convert.ToInt32(element.ToString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM