C# 在C中创建动态字符串数组,并通过循环将字符串(拆分方法的结果)添加到两个单独的数组中
我有一个字符串列表,其中包括以下格式的字符串:xx@yyC# 在C中创建动态字符串数组,并通过循环将字符串(拆分方法的结果)添加到两个单独的数组中,c#,arrays,string,list,split,C#,Arrays,String,List,Split,我有一个字符串列表,其中包括以下格式的字符串:xx@yy xx = feature name yy = project name 基本上,我想在@处拆分这些字符串,并将xx部分存储在一个字符串数组中,将yy部分存储在另一个字符串数组中,以执行进一步的操作 string[] featureNames = all xx here string[] projectNames = all yy here 我可以在foreach或C中的for循环中使用split方法string.split'@'分
xx = feature name
yy = project name
基本上,我想在@处拆分这些字符串,并将xx部分存储在一个字符串数组中,将yy部分存储在另一个字符串数组中,以执行进一步的操作
string[] featureNames = all xx here
string[] projectNames = all yy here
我可以在foreach或C中的for循环中使用split方法string.split'@'分割字符串,但我不能在两个不同的字符串数组中分别存储两个部分,不一定是数组,但列表也可以工作,因为以后可以将其转换为数组
主要的问题是在拆分后确定字符串的两个部分,然后将它们分别附加到字符串数组中。以下内容如何:
List<String> xx = new List<String>();
List<String> yy = new List<String>();
var strings = yourstring.Split('@');
xx.Add(strings.First());
yy.Add(strings.Last());
这是一个简单的方法:
var xx = new List<string>();
var yy = new List<string>();
foreach(var line in listOfStrings)
{
var split = string.split('@');
xx.Add(split[0]);
yy.Add(split[1]);
}
public class ProjectFeature
{
public readonly string Project;
public readonly string Feature;
public ProjectFeature(string project, string feature)
{
this.Project = project;
this.Feature = feature;
}
public static IEnumerable<ProjectFeature> ParseList(IEnumerable<string> input)
{
return input.Select(v =>
{
var split = v.Split('@');
return new ProjectFeature(split[1], split[0]);
}
}
}
上面例示了xx和yy的列表,循环遍历字符串列表,并对每个字符串进行拆分。然后将分割结果添加到先前实例化的列表中
var featureNames = new List<string>();
var productNames = new List<string>();
foreach (var productFeature in productFeatures)
{
var parts = productFeature.Split('@');
featureNames.Add(parts[0]);
productNames.Add(parts[1]);
}
主要问题是在拆分后确定字符串的两个部分,然后将它们分别附加到字符串数组中
为什么使用不同的阵列?字典不是更合适吗
List<String> input = File.ReadAllLines().ToList<String>(); // or whatever
var output = new Dictionary<String, String>();
foreach (String line in input)
{
var parts = input.Split('@');
output.Add(parts[0], parts[1]);
}
foreach (var feature in output)
{
Console.WriteLine("{0}: {1}", feature.Key, feature.Value);
}
您可以这样做:
var splits = input.Select(v => v.Split('@'));
var features = splits.Select(s => s[0]).ToList();
var projects = splits.Select(s => s[1]).ToList();
如果您不介意代码稍微多一些,但性能更好,对垃圾收集器的压力更小,那么:
var features = new List<string>();
var projects = new List<string>();
foreach (var split in input.Select(v => v.Split('@')))
{
features.Add(split[0]);
projects.Add(split[1]);
}
怎么样
List<string> lst = ... // your list containging xx@yy
List<string> _featureNames = new List<string>();
List<string> _projectNames = new List<string>();
lst.ForEach(x =>
{
string[] str = x.Split('@');
_featureNames.Add(str[0]);
_projectNames.Add(str[1]);
}
string[] featureNames = _featureNames.ToArray();
string[] projectNames = _projectNames.ToArray();
试试这个
var ls = new List<string>();
ls.Add("123@project");
ls.Add("123@project1");
var f = from c in ls
select new
{
XX = c.Split("@")[0],
YY = c.Split("@")[1]
};
string [] xx = f.Select (x => x.XX).ToArray();
string [] yy = f.Select (x => x.YY).ToArray();
请出示您当前的密码。为什么要否决这个问题。新手不能问点什么,尽管现在看起来很愚蠢。每个人都有一个学习阶段,他们会遇到这样的问题。即便如此,至少要给出理由否决这个问题。不幸的是,有些人会这样做。几乎,虽然有一个字符串列表要拆分,而不仅仅是一个。是的,但我以后需要在不同的情况下使用xx和yy。我真的不知道这一切是如何工作的,所以尽量保持它的简单,以避免复杂的编码。谢谢。通过这些想法,我学到了很多新东西。@Chetan您可以始终迭代字典的属性,以检索实际上包含与featureNames数组相同的所有键。此外,使用字典,只需将一个变量传递给其他函数,而不必同时传递两个数组。是的,正确,得到了。查看有关词典的更多信息。再次感谢。这真的很有帮助。太好了,这正是我在这里做的。我的windows窗体应用程序中有一个treeView。最后,我检查了如下节点xx@yy名称我正在编写一个单独的类,在这个类中,我可以将此列表作为输入,然后使用功能和项目名称从数据库中检索所需的数据,然后将它们添加到XML或Excel,甚至添加到文本文件中,最终用户将选择何种格式作为输出。非常感谢你。我想学习这些技巧需要一些时间,但我现在也有足够的动力。
List<string> lst = ... // your list containging xx@yy
List<string> _featureNames = new List<string>();
List<string> _projectNames = new List<string>();
lst.ForEach(x =>
{
string[] str = x.Split('@');
_featureNames.Add(str[0]);
_projectNames.Add(str[1]);
}
string[] featureNames = _featureNames.ToArray();
string[] projectNames = _projectNames.ToArray();
var ls = new List<string>();
ls.Add("123@project");
ls.Add("123@project1");
var f = from c in ls
select new
{
XX = c.Split("@")[0],
YY = c.Split("@")[1]
};
string [] xx = f.Select (x => x.XX).ToArray();
string [] yy = f.Select (x => x.YY).ToArray();