0

My front-end application sends strings that look like this:

"12-15" 

to a back-end C# application.

Can someone give me some pointers as to how I could extract the two numbers into two variables. Note the format is always the same with two numbers and a hyphen between them.

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Alan2
  • 23,493
  • 79
  • 256
  • 450

9 Answers9

3
 string stringToSplit = "12-15";
 string[] splitStringArray;
 splitStringArray = stringToSplit.Split('-');

splitStringArray[0] will be 12

splitStringArray[1] will be 15

JohnG
  • 9,259
  • 2
  • 20
  • 29
2
int[] numbers = "12-15".Split('-')
        .Select(x => {
            int n;
            int.TryParse(x, out n);
            return n;
        })
        .ToArray();
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Dan
  • 2,818
  • 23
  • 21
2

Split the string into parts:

string s = "12-15";
string[] num = s.Split('-');
int part1 = Convert.ToInt32(num[0]);
int part2 = Convert.ToInt32(num[1]);
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
1

We call Split on a string instance. This program splits on a single character

string s ="12-15";
string[] words = s.Split('-');
foreach (string word in words)
{
    int convertedvalue = Convert.ToInt32(word ); 
    Console.WriteLine(word);
}

string[] ss= s.Split('-');
int x = Convert.ToInt32(ss[0]);
int y = Convert.ToInt32(ss[1]);

more info

MMM
  • 3,132
  • 3
  • 20
  • 32
1

Here is the correct version without the wrong code

string textReceived = "12-15";

string[] numbers = textReceived.Split('-');
List<int> numberCollection = new List<int>();
foreach (var item in numbers)
{
    numberCollection.Add(Convert.ToInt32(item));
}
1

You can use the below code to split and it will return string for each value, then you can typecast it to any type you wish to ...

string myString = "12-15-18-20-25-60";
string[] splittedStrings = myString.Split('-');
foreach (var splittedString in splittedStrings)
{
    Console.WriteLine(splittedString + "\n");
}
Console.ReadLine();
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
ankitmvp
  • 33
  • 4
0
String numberString = "12-15" ;
string[] arr = numberString.Split("-");

Now you will get a string array , you can use parsing to get the numbers alone

int firstNumber = Convert.ToInt32(arr[0]);

Helpful answer related to parsing :

https://stackoverflow.com/a/199484/5395773

Community
  • 1
  • 1
Venkat
  • 2,549
  • 2
  • 28
  • 61
0
string str = null;
string[] strArr = null;
int count = 0;
str = "12-15";
char[] splitchar = { '-' };

strArr = str.Split(splitchar);
for (count = 0; count <= strArr.Length - 1; count++)
{
    MessageBox.Show(strArr[count]);
}
Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Asifuzzaman Redoy
  • 1,773
  • 1
  • 15
  • 30
0

You could convert that string explicitly to an integer array using Array.ConvertAll method and access the numbers using their index, you can run the below example here.

using System;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var number = "12-15";
            var numbers = Array.ConvertAll(number.Split('-'), int.Parse);
            Console.WriteLine(numbers[0]);
            Console.WriteLine(numbers[1]);
        }
    }
}

Or you can explicitly convert the numeric string using int.Parse method, the int keyword is an alias name for System.Int32 and it is preffered over the complete system type name System.Int32, you can run the below example here.

using System;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var number = "12-15";
            var numbers = number.Split('-');
            var one = int.Parse(numbers[0]);
            var two = int.Parse(numbers[1]);
            Console.WriteLine(one);
            Console.WriteLine(two);
        }
    }
}

Additional read: Please check int.Parse vs. Convert.Int32 vs. int.TryParse for more insight on parsing the input to integer

Community
  • 1
  • 1
Abdul Mateen Mohammed
  • 1,864
  • 1
  • 12
  • 21