你的問題與散列表無關。你有一個編譯錯誤,因爲在.NET中字符串是不可變的。
其次,這是不相關的,foreach循環變量不能分配給。
所以,與其
foreach (string klucevi in *whatever*)
{
if (klucevi[0] == 'a')
{
klucevi[0] = 'A';
}
Console.WriteLine(klucevi);
}
使用
foreach (string klucevi in *whatever*)
{
var temp = klucevi;
if (temp[0] == 'a')
{
StringBuilder sb = new StringBuilder(temp);
sb[0] = 'A';
temp = sb.ToString();
}
Console.WriteLine(temp);
}
不要忘了包括using System.Text;
聲明。
更新: 上面的aswer顯示了一種通用的方式來修改.NET中的字符串,而不僅僅是替換一個字符。
此外,一些人對這種方法的有效性表示擔憂。他們錯了。更多信息請見Strings Undocumented。
更新2: 我喜歡受到挑戰。雖然這對於手頭的問題來說完全沒有關係,但討論已經結束了關於使用StringBuilder的效率,與使用"A" + temp.Substring(1)
相比。因爲我喜歡事實,並且我假設有些讀者會同意,所以我跑了一點基準。
我使用.NET 4.5作爲32位和64位進程在64位Windows 7機器上運行測試。事實證明,StringBuilder方法總是比替代方法快20%左右。內存使用情況大致相同。因人而異。
對於那些誰關心重複測試,這裏的源代碼:
using System;
using System.Diagnostics;
using System.Text;
static class Program
{
static void Main(string[] args)
{
for (int length = 50; length <= 51200; length = length * 2)
{
string input = new string(' ', length);
// warm up
PerformTest(input, 1);
// actual test
PerformTest(input, 100000);
}
}
static void PerformTest(string input, int iterations)
{
GC.Collect();
GC.WaitForFullGCComplete();
int gcRuns = GC.CollectionCount(0);
Stopwatch sw = Stopwatch.StartNew();
for (int i = iterations; i > 0; i--)
{
StringBuilder sb = new StringBuilder(input);
sb[0] = 'A';
input = sb.ToString();
}
long ticksWithStringBuilder = sw.ElapsedTicks;
int gcRunsWithStringBuilder = GC.CollectionCount(0) - gcRuns;
GC.Collect();
GC.WaitForFullGCComplete();
gcRuns = GC.CollectionCount(0);
sw = Stopwatch.StartNew();
for (int i = iterations; i > 0; i--)
{
input = "A" + input.Substring(1);
}
long ticksWithConcatSubstring = sw.ElapsedTicks;
int gcRunsWithConcatSubstring = GC.CollectionCount(0) - gcRuns;
if (iterations > 1)
{
Console.WriteLine(
"String length: {0, 5} With StringBuilder {1, 9} ticks {2, 5} GC runs, alternative {3, 9} ticks {4, 5} GC Runs, speed ratio {5:0.00}",
input.Length,
ticksWithStringBuilder, gcRunsWithStringBuilder,
ticksWithConcatSubstring, gcRunsWithConcatSubstring,
((double)ticksWithStringBuilder)/((double)ticksWithConcatSubstring));
}
}
}
什麼是你得到的錯誤? – Josh
錯誤:索引器'string.this [int]'的屬性不能分配給 - 它是隻讀的。 – user2699298
這是因爲字符串是不可變的。你不能像這樣改變鑰匙。 –