• Splitting Strings
    3 replies, posted
I'm trying to split this string variable by the first character and then convert it to an iteger but I can't seem to remember the code necessary,sorry to make a post on such trivial matters. [CODE] using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnQ1_Click(object sender, EventArgs e) { //Variables string accountnumber; int checknumber; //Get number accountnumber=Microsoft.VisualBasic.Interaction.InputBox("Enter the account number","Account Validation","",300,300); //Get checknumber checknumber = int.Parse(accountnumber.Substring(1)); MessageBox.Show(checknumber.ToString()); } } } [/CODE]
[cpp]int firstCharAsNumber = Convert.ToInt32(accountnumber.Substring(0, 1));[/cpp]
Just access the first element in the string, in C++ you use the [] operator. Set an int to that element like so. std::string string = "Facepunch.\n"; int a = string[0]
[QUOTE=jmanmc;17983553]Just access the first element in the string, in C++ you use the [] operator. Set an int to that element like so. std::string string = "Facepunch.\n"; int a = string[0][/QUOTE] That doesn't convert it to an integer in either C# or C++.
Sorry, you need to Log In to post a reply to this thread.