Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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#_Regex_Linq_Stream - Fatal编程技术网

C# 将一行字符串拆分为由两个字符组成的字符串

C# 将一行字符串拆分为由两个字符组成的字符串,c#,regex,linq,stream,C#,Regex,Linq,Stream,我正在阅读一个文本文件,该文件由字母数字值组成,如下所示 IQQQQQ WG2223 S22228 D22223 目前,这些值是循环的,每个字符被发送到一个开关盒。然后,此开关读取该字符并输出给定结果。此过程的代码如下所示 private void LoadLevel(Stream stream) { List<string> lines = new List<string>(); uint width; using (StreamRea

我正在阅读一个文本文件,该文件由字母数字值组成,如下所示

IQQQQQ
WG2223
S22228
D22223
目前,这些值是循环的,每个字符被发送到一个开关盒。然后,此开关读取该字符并输出给定结果。此过程的代码如下所示

private void LoadLevel(Stream stream)
{
     List<string> lines = new List<string>();
     uint width;

     using (StreamReader reader = new StreamReader(stream))
     {
           string line = reader.ReadLine();
           width = (uint)line.Length;

           while (line != null)
           {
                lines.Add(line);    
                line = reader.ReadLine();
           }
    }

    tiles = new Tile[width, lines.Count];

    for (int y = 0; y < Height; ++y)
    {
        for (int x = 0; x < Width; ++x)
        {
             char type = lines[y][x];
             tiles[x, y] = LoadTile(type, x, y);
        }
    }
}
我想将这一行拆分为三个字符串,每个字符串两个字符长,然后将其传递给LoadTile,然后继续在网格的其余部分循环。然而,我不知道从哪里开始尝试完成这项任务。任何帮助都将不胜感激。

您可以这样做:

var str = "IQQQQQ";
var arr = Regex.Matches(str, "..").Cast<Match>().ToArray().Select(u => u.Value);
// arr[0] = "IQ" 
// arr[1] = "QQ"
// arr[2] = "QQ"

首先,您可能希望将LoadTile的签名从LoadTilechar,int,int更改为LoadTilestring,int,int。然后将宽度的计算更改为

width = line.Length/2;
当然,如果任何一行有奇数个字符,您将丢失最后一个字符。此外,如果第一行之后的任何一行较短,则会出现异常;如果第一行较长,则会丢失额外的数据

然后你可以像这样循环并取子串

for (int y = 0; y < Height; ++y)
{
    for (int x = 0; x < Width; ++x)
    {
         string type = lines[y].Substring(x*2,2);
         tiles[x, y] = LoadTile(type, x, y);
    }
}

这是获取子字符串的方式:

var item = source.Substring(i * 2, 2);
可以在一行中拆分为阵列:

var split = Enumerable.Range(0,3).Select(i=>source.Substring(i * 2, 2)).ToArray();

for (var i = 0; i < 3; i++)
{
    var item = split[i];
    // ...
}

使用LinQ。尝试对每一行执行此操作:

//Converts the string to a List of strings
List<string> stringArray = line.ToCharArray().Select(c=>c.ToString()).ToList();
//Iterates over the list and concats every pair of elements
List<string> result = stringArray.Select((value, index) => new { value, index })
    .GroupBy(x => x.index / 2, x => x.value).Select(pair => pair.Aggregate((s1,s2) => s1 + s2)).ToList();

正则表达式似乎对于this@DavidG我知道,但是,尽管我很好地编写了C,当我有IDE时,我不记得很多琐碎方法的签名,因为我不像在JS中那样用C编写很多代码,但我知道Regex类,所以结果是:试试LinqPad,它非常适合快速编写C和VB.Net代码。是的,如果您还想要IntelliSense,它是免费的,但非常便宜。@DavidG我开始工作时肯定会购买:我接受您的评论作为答案,因为您的解决方案是围绕现有方法实现的最快的解决方案。
//Converts the string to a List of strings
List<string> stringArray = line.ToCharArray().Select(c=>c.ToString()).ToList();
//Iterates over the list and concats every pair of elements
List<string> result = stringArray.Select((value, index) => new { value, index })
    .GroupBy(x => x.index / 2, x => x.value).Select(pair => pair.Aggregate((s1,s2) => s1 + s2)).ToList();