Solved

Splitting String into Int

Posted on 2014-02-10
3
382 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now