Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Splitting String into Int

Posted on 2014-02-10
Medium Priority
393 Views
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

0
Question by:andyw27
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 19

Accepted Solution

regmigrant earned 2000 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

ID: 39849266
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 51

Expert Comment

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

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
###### Suggested Courses
Course of the Month6 days, 11 hours left to enroll