简体   繁体   English

将字符串转换为多维char数组C#

[英]Converting string into multidimensional char array c#

this string is given : 该字符串给出:

S#####
.....#
#.####
#.####
...#.G
##...#

and I want to convert it into a 6x6 char array.I know that I can fill the fields like this : 我想将其转换为6x6 char数组。我知道我可以像这样填充字段:

    charName[0,0] ='S';
    charName[0,1] ='#'; 
    charName[0,2] ='#';
    charName[0,3] ='#';
    charName[0,4] ='#';
    charName[0,5] ='#';

However this cost much time and I think there is a better way to do it. 但是,这花费了很多时间,我认为有更好的方法可以做到这一点。 Thank you for any kind of help :) 谢谢您的任何帮助:)

Simply split by newlines: 只需换行即可:

var s="...";       // your initial string
var arr=s.Split(new[] { Environment.NewLine });

// you can then get your characters like:
var ch=arr[0][2];  // would be # in your example

It is not very clear what you are asking, but I guess you are asking for the way to initialise array. 还不清楚您要问什么,但是我想您正在问初始化数组的方法。 So this is how: 因此,方法如下:

    char[,] charName = new char[6,6] {
        {'S', '#', '#', '#', '#', '#' },
        {'.', '.', '.', '.', '.', '#' },
        {'#', '.', '#', '#', '#', '#' },
        {'#', '.', '#', '#', '#', '#' },
        {'.', '.', '.', '#', '.', 'G' },
        {'#', '#', '.', '.', '.', '#' },
    };

Though this will not work if your data is arbitrary. 尽管如果您的数据是任意的,这将不起作用。

Can you try this one.. 你能试试这个吗..

        static void Main(string[] args)
        {
            char[,] charArray = new char[6, 6];
            Add(0, ref charArray, 'S','#','#','#','#','#');
            Add(1, ref charArray, '.', '.', '.', '.', '.', '#'); 
            Add(2, ref charArray, '#', '.', '#', '#', '#', '#');
            Add(3, ref charArray, '#', '.', '#', '#', '#', '#');
            Add(4, ref charArray, '.', '.', '.', '#', '.', 'G');
            Add(5, ref charArray, '#', '#', '.', '.', '.', '#');
        }

        public static void Add(int index, ref char[,] array, params char[] parameters)
        {
            for (int i = 0; i < parameters.Length; i++)
            {
                array[index, i] = parameters[i];
            }
        }

I take it as your string is a multiline one. 我认为这是因为您的字符串是多行的。 So it must have '\\n' at the end of each line except the last one. 因此,除最后一行外,每行末尾必须带有“ \\ n” So you first should split the string into a array of lines. 因此,您首先应该将字符串拆分为一系列行。 Then from each line take each character and put it into the character array. 然后从每一行中取出每个字符并将其放入字符数组。

Here it is, 这里是,

string input = "S#####\n.....#\n#.####\n#.####\n...#.G\n##...#";

char[,] charArray = new char[6, 6];

var lines = input.Split(new [] { '\n' });
int row = 0;
foreach (string line in lines)
{
    int column = 0;
    foreach (char character in line)
    {
       charArray[row, column] = character;
       column++;
    }
    row++;
}
Console.ReadKey();

Finally the charArray (multidimensional array) will hold your string characters. 最后, charArray (多维数组)将保存您的字符串字符。

//Single-string matrix:
            String input = @"S#####
                            .....#
                            #.####
                            #.####
                            ...#.G
                            ##...#";

            var arr = input.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);


            //Result "S" will be arr[0][0].ToString()
            ////Result "#" will be arr[0][0].ToString()

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

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