2012-10-08 124 views
1

添加(拆分方法的結果)的字符串分成兩個單獨的陣列我有字符串的列表,其包括在格式字符串:XX @ YY創建在C#動態字符串數組,並通過循環

xx = feature name 

yy = project name 

基本上,我想在@處分割這些字符串,並將xx部分存儲在一個字符串數組中,並將yy部分存儲在另一個字符串數組中以執行進一步的操作。

string[] featureNames = all xx here 

string[] projectNames = all yy here 

我能夠分裂在foreach或在C#循環使用分割法(string.split(「@」))的字符串,但我不能在兩個不同的字符串數組分別存儲兩個部分(不一定是數組,但是列表也可以,因爲之後可以將其轉換爲數組)。

主要問題是確定分割後的字符串的兩個部分,然後將它們分別附加到字符串數組。

+1

請顯示您的當前代碼。 – CodeCaster

+0

這些對如何分隔? – Jodrell

+2

爲什麼選擇投票的問題。新手不能要求什麼,儘管目前看起來很愚蠢。 每個人都有一個學習的階段,他們遇到了這樣的問題。如果是這樣,至少給理由投票的問題。傷心有些人這樣做。 – Indigo

回答

5

這是一個簡單的方法:

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]); 
} 

以上實例的xx和列表和yy列表,遍歷字符串列表和爲每一個分裂它。然後它將分割的結果添加到先前實例化的列表中。

3

如何如下:

List<String> xx = new List<String>(); 
List<String> yy = new List<String>(); 
var strings = yourstring.Split('@'); 

xx.Add(strings.First()); 
yy.Add(strings.Last()); 
+0

幾乎,儘管有一個_list_的字符串要分割,而不僅僅是一個。 – Oded

2
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]); 
} 
1
var all_XX = yourArrayOfStrings.Select(str => str.split('\@')[0]); // this will be IENumerable 
var all_YY = yourArrayOfStrings.Select(str => str.split('\@')[1]); // the same fot YY. But here make sure that element at [1] exists 
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); 
} 
+0

是的,但我需要在以後的不同情況下使用xx和yy。我不確定所有這些工作是如何實現的,儘可能簡單以避免複雜的編碼。非常感謝。通過所有這些想法,我正在學習許多新事物。 – Indigo

+0

@Chetan你總是可以遍歷字典的['Keys'](http://msdn.microsoft.com/en-us/library/bb337739.aspx)屬性來檢索所有的鍵(這實際上包含與你的featureNames數組相同)。此外,使用字典,您只需將一個變量傳遞給其他函數而不是兩個數組。 – CodeCaster

+0

是的,正確的,明白了。查看更多關於Dictionary的信息。再次感謝。這真的很有幫助。 – Indigo

2

你可以做這樣的事情:

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]); 
} 

但總體我建議創建類並解析你的輸入(更多C#風格的方法):

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]); 
     } 
    } 
} 

,並在以後使用它(只是可能使用的例子):

var projectFeatures = ProjectFeature.ParseList(File.ReadAllLines(@"c:\features.txt")).ToList(); 
var features = projectFeatures.Select(f => f.Feature).ToList(); 
var projects = projectFeatures.Select(f => f.Project).ToList(); 
// ??? etc. 
+0

是啊。這個看起來不錯 –

+0

完美,這正是我在這裏做的。我在我的Windows窗體應用程序中有一個樹視圖。最後,我將檢查節點作爲這個xx @ yy名稱。我正在編寫一個單獨的類,我可以將該列表作爲輸入,然後使用功能和項目名稱從數據庫中檢索所需的數據,然後將它們添加到XML或Excel甚至是文本文件,無論最終用戶的格式如何選擇輸出。非常感謝。我想這需要一些時間來學習這些技巧,但我現在也有足夠的動力。 – Indigo

2

如何

List<string> lst = ... // your list containging [email protected] 

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(); 
1

試試這個。

var ls = new List<string>(); 
ls.Add("[email protected]"); 
ls.Add("[email protected]"); 

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();