简体   繁体   English

如何将“ 12-15”这样的字符串分成两个数字?

[英]How can I split a string like “12-15” into two numbers?

My front-end application sends strings that look like this: 我的前端应用程序发送的字符串如下所示:

"12-15" 

to a back-end C# application. 到后端C#应用程序。

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. 请注意,格式始终相同,两个数字之间带有连字符。

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

splitStringArray[0] will be 12 splitStringArray [0]将为12

splitStringArray[1] will be 15 splitStringArray [1]将为15

int[] numbers = "12-15".Split('-')
        .Select(x => {
            int n;
            int.TryParse(x, out n);
            return n;
        })
        .ToArray();

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]);

We call Split on a string instance. 我们在字符串实例上调用Split。 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 更多信息

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));
}

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();
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 https://stackoverflow.com/a/199484/5395773

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]);
}

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 . 您可以使用Array.ConvertAll方法将该字符串显式转换为整数数组,并使用其索引访问数字,您可以在此处运行以下示例。

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 . 或者,您可以使用int.Parse方法显式转换数字字符串,int关键字是System.Int32的别名,并且它具有完整的系统类型名称System.Int32 ,您可以在此处运行以下示例。

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 其他阅读:请检查int.ParseConvert.Int32int.TryParse以获取有关将输入解析为整数的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM