Solved

Splitting String into Int

Posted on 2014-02-10
3
390 Views
Last Modified: 2014-03-26
Hi,

I have a simple string 35,1

Which is separated by a comma.

Taking this string I need to output two int’s

X = 35
Y = 1

Is this possible please?
0
Comment
Question by:andyw27
3 Comments
 
LVL 19

Accepted Solution

by:
regmigrant earned 500 total points
ID: 39847698
Heres a routine that will split the two on the comma into two elements of a string array then uses int.parse to produce the numbers.  NB: this will error if the numbers cannot parse to an integer - you can use int.tryparse(word) to catch such situations

 
using System;

class Program
{
    static void Main()
    {
      string s = "35,1";
      //
      // Split string on commas returns both elements as a string array
      //
      string[] words = s.Split(',');
      foreach (string word in words)
      {
                     num = int.parse(word)
          Console.WriteLine(num);
      }
    }
}
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 39849266
hi.. this is your solution
string str = "35,1";

string [ ] str1 = str.split(",");
int n1 = convert.toInt(str1[0].toString());
int n2 = convert.toInt(str1[1].toString());

try this I hope it's working...
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39861416
You can also use one-liners:

string v = "35,1";
int x = Convert.ToInt32(v.Split(',')[0]);
int y = Convert.ToInt32(v.Split(',')[1]);

/gustav
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question