如何在C#中的动态数组 [英] how to create dynamic array in c#

查看:157
本文介绍了如何在C#中的动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码,我明确地指定服务器的名字,但我想暗中指定的服务器名称。因此,我想代码隐含&放取服务器名称;删除 \\ &安培; \\\
在其中。

 的System.Diagnostics.Process P =新的System.Diagnostics.Process(); 
p.StartInfo.FileName =CMD;
p.StartInfo.UseShellExecute = FALSE;
p.StartInfo.Arguments =/ C网的观点;
的char [] delimiterChars = {'\\'};
列输入=\\st1\\\
,\\st10\\\
,\\st4\\\
,\\st5
名单,LT;字符串> servernames节点= input.Split(,)了ToList();
Console.WriteLine(输入);
到Console.ReadLine();


解决方案

我不明白你在说什么动态数组,但我觉得我得到你想要做什么。下面是一些代码,将拆分输入,返回所有元素的列表没有前导 \\ 或尾随 \\\

 列表<串GT; servernames节点= input.Split(,)
。选择(S => {
如果(s.StartsWith(@\\))
S = s.Substring( 2);
如果(s.EndsWith(\\\
))
S = s.Substring(0,s.Length - 1);
返回小号;
} )
.ToList();


In my code, I explicitly specify server names, but I want to implicitly specify server names. Hence, I would like code to take server names implicitely & remove \\ & \n in them.

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd ";
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = "/C net view";
char[] delimiterChars = { '\\' };
string input = "\\st1\n,\\st10\n,\\st4\n,\\st5";
List<string> serverNames = input.Split(',').ToList();
Console.WriteLine(input);
Console.ReadLine();

解决方案

I don't understand what you're saying about "dynamic arrays", but I think I get what you're trying to do. Here's some code that will split your input and return a list of all the elements without a leading \\ or trailing \n:

List<string> serverNames = input.Split(',')
                                .Select(s => {
                                    if (s.StartsWith(@"\\"))
                                        s = s.Substring(2);
                                    if (s.EndsWith("\n"))
                                        s = s.Substring(0, s.Length - 1);
                                    return s;
                                 })
                                .ToList();

这篇关于如何在C#中的动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆