Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中获得两个用冒号分隔的数字?_C#_.net_String_Split_Colon - Fatal编程技术网

如何在C#中获得两个用冒号分隔的数字?

如何在C#中获得两个用冒号分隔的数字?,c#,.net,string,split,colon,C#,.net,String,Split,Colon,我有点问题。我正在尝试拆分一些文本和数字 考虑这个字符串输入 string input = "1 Johannes 1:3, 2 Johannes 2:6, 1 Mosebok 5:4"; 我想把2:3,2:6,5:4从课文的其余部分分开。 然后我希望它们与冒号分开,并添加到列表中 列表在循环之后会是这样的。 2. 3. 2. 6. 5. 四, 在其中,我可以从[0]和[1]、[2]和[3]、[4]和[5]创建哈希表条目 我要感谢你们所有人对此的反馈 使用正则表达式 using System;

我有点问题。我正在尝试拆分一些文本和数字

考虑这个字符串输入

string input = "1 Johannes 1:3, 2 Johannes 2:6, 1 Mosebok 5:4";
我想把2:3,2:6,5:4从课文的其余部分分开。 然后我希望它们与冒号分开,并添加到列表中

列表在循环之后会是这样的。 2. 3. 2. 6. 5. 四,

在其中,我可以从[0]和[1]、[2]和[3]、[4]和[5]创建哈希表条目

我要感谢你们所有人对此的反馈

使用正则表达式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "1 Johannes 1:3, 2 Johannes 2:6, 1 Mosebok 5:4";

            string pattern = @"(?'index'\d+)\s+(?'name'\w+)\s+(?'para'\d+:\d+),?";
            MatchCollection matches = Regex.Matches(input, pattern);
            foreach (Match match in matches)
            {
                Console.WriteLine("index : {0}, name : {1}, para : {2}",
                    match.Groups["index"],
                    match.Groups["name"],
                    match.Groups["para"]
                );
            }
            Console.ReadLine();
        }
    }
}

下面是一些可以帮助您的代码

        var input = "1 Johannes 1:3, 2 Johannes 2:6, 1 Mosebok 5:4";
        var nextIndex = 0;
        var currentIndex = 0;
        var numbers = new List<int>();

        do
        {
            currentIndex = input.IndexOf(":", currentIndex + 1);
            var leftNumber = Convert.ToInt32(input[currentIndex - 1].ToString());
            var rightNumber = Convert.ToInt32(input[currentIndex + 1].ToString());

            numbers.Add(leftNumber);
            numbers.Add(rightNumber);

            nextIndex = input.IndexOf(":", currentIndex + 1);

        } while (nextIndex != -1);
var input=“1约翰内斯1:3,2约翰内斯2:6,1莫斯科5:4”;
var-nextIndex=0;
var currentIndex=0;
变量编号=新列表();
做
{
currentIndex=input.IndexOf(“:”,currentIndex+1);
var leftNumber=Convert.ToInt32(输入[currentIndex-1].ToString());
var rightNumber=Convert.ToInt32(输入[currentIndex+1].ToString());
number.Add(leftNumber);
编号。添加(rightNumber);
nextIndex=input.IndexOf(“:”,currentIndex+1);
}而(nextIndex!=-1);

如果我正确理解你的问题,我会按照你的要求去做

string input = "1 Johannes 1:3, 2 Johannes 2:6, 1 Mosebok 5:4";
var table = Regex.Matches(input, @"(\d+):(\d+)")
                .Cast<Match>()
                .ToLookup(m => m.Groups[1].Value, m => m.Groups[2].Value);
string input=“1约翰内斯1:3,2约翰内斯2:6,1莫斯科5:4”;
var table=Regex.Matches(输入,@“(\d+):(\d+))
.Cast()
.ToLookup(m=>m.Groups[1].Value,m=>m.Groups[2].Value);

向我们展示您迄今为止所做的尝试以及您遇到的任何错误,我们将为您提供帮助。不幸的是,StackOverflow不是一个为我写代码的网站。一个关于“用c#拆分字符串”的非常简单的谷歌搜索将得到你的方法,结合一个简单的逻辑和呼啦!解决方案需要有多具体?如果输入总是以这种形式出现,您可以简单地找到冒号的位置,然后假设前面和后面的字符是您需要获得的。但是如果有大于一位数的数字,那就不同了。
[\d]:[\d]
要拯救的正则表达式。使用regex
Match
方法。您可以根据“,”使用LastIndexOf搜索最后一个空格,然后根据“:”再次拆分。您的输入结构如何?