[英]C# - Insert a variable number of spaces into a string? (Formatting an output file)
[英]c# make elements of list of string unique, by appending variable number of spaces
我遇到了一個小算法問題,因此無法實現(快速)實現。 實際上,我的工作尚未完成,但是已經在減慢DataGridView的負載。
最初的問題:WinForms的DataGridView有一個2005年以來的錯誤(顯然直到VS2015仍未解決),該錯誤使具有相同名稱的列的錯誤綁定不區分大小寫。 更精確地說,如果您有2列“ Cat”和“ cat”,它們將都綁定到數據庫中的同一對象(首次找到)。
無論如何,我正在使用ITypedList和GetItemProperties()通知DGV我要鏈接的字段。 這個想法(在stackoverflow的某處可見)是在“不區分大小寫的重復”列的名稱之后添加空格,如下所示:
"cat" --> leave as is
"Cat" --> needs to be come "Cat_" _ means space
"cAt" --> needs to become "cAt__" __ means two spaces and so on
算法問題:使用循環將字符串添加到列表中。 在添加之前,請檢查字符串是否存在(修剪和區分大小寫),如果存在,請在名稱末尾添加一個空格。 保持大小寫不變。 換句話說,通過在名稱后添加n個空格來使字符串唯一。
希望我描述得很好,任何想法都值得贊賞。
我已經用變體和速度進行了一些測試,也許還對DGV觸發GetItemProperties()回調5次或更多的事實做了測試。
這是我的代碼:
public partial class Form1 : Form
{
List<string> list = new List<string>
{
"cat", // the cat
"Cat", // first duplicate needs to become Cat_ (one space)
"kitty",
"kittY",
"dog",
"cAt", // third duplicate needs to become cAt__ (two spaces)
"Dog",
"monkey",
"monKey",
"Monkey",
};
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
AnimalHeaders phList = new AnimalHeaders();
for (int i = 0; i < list.Count(); i++)
{
string field = list.ElementAt(i);
var caseInsenList = phList.Where(z => z.Name.Trim().Equals(field, StringComparison.OrdinalIgnoreCase));
int count = caseInsenList.Count();
if (count == 0) // no results
{
phList.Add(new AnimalHeader { Name = field });
}
else // exists but can be many
{
for (j = 0; j < count; j++)
{
string found = caseInsenList.ElementAt(j).Name.Trim(); // no spaces
if (field == found)
continue; // exact match, case sensitive, we already have this, skip
else
{
}
}
}
}
}
}
public class AnimalHeader
{
public string Name { get; set; }
public Type Type { get; set; }
public int Order { get; set; }
}
public class AnimalHeaders : List<AnimalHeader>
{
}
嘗試一個簡單的Linq :我們將相同的項目分組 ,然后將index
空間(下層范圍)添加到組中的每個index
項目。 最后,我們拼合 (合並)所有組。
List<string> list = new List<string>() {
"cat", // the cat
"Cat", // first duplicate needs to become Cat_ (one space)
"kitty",
"kittY",
"dog",
"cAt", // third duplicate needs to become cAt__ (two spaces)
"Dog",
"monkey",
"monKey",
"Monkey",
};
List<string> result = list
.GroupBy(item => item, StringComparer.OrdinalIgnoreCase)
.SelectMany(chunk => chunk
.Select((item, index) => string.Format("{0}{1}", item, new string('_', index))))
.ToList();
演示:
Console.WriteLine(string.Join(Environment.NewLine, result));
結果:
cat
Cat_
cAt__
kitty
kittY_
dog
Dog_
monkey
monKey_
Monkey__
編輯:如果我們要保留 初始順序,我們必須將其存儲(初始列表中項目的index
),最后按它進行排序 :
List<string> result = list
.Select((value, index) => new {
value,
index
})
.GroupBy(item => item.value, StringComparer.OrdinalIgnoreCase)
.SelectMany(chunk => chunk
.Select((item, index) => new {
value = string.Format("{0}{1}", item.value, new string('_', index)),
index = item.index
}))
.OrderBy(item => item.index)
.Select(item => item.value)
.ToList();
結果:
cat
Cat_
kitty
kittY_
dog
cAt__
Dog_
monkey
monKey_
Monkey__
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.