我有以下代碼:foreach循環:(?)循環變量不能分配後進行修改
string STR = "AA ABC AA AA ABC aa aa aa AA" ; //declare string
Regex rx = new Regex(@"AA"); // declare regular expression
MatchCollection matches = rx.Matches(STR); // find matches in STR
foreach (Match match in matches)
{
// perform sub-string operations that changes the original string
STR = "AA AA ABC aa aa aa AA" // substring operation which arbitrary changes the string
matches = rx.Matches(STR); // perform matching operation again
// because original string is changed
// ERROR : 'matches' in for loop is not changed (?)
// Question: how can I change 'matches' in for loop, thus it will start
// to work in new modified string ?
}
任何人可以幫助我上面的解決?
編輯:
int j = 15
for (int i = 0 to j){
// change both i and j value arbitrarily
i = 100
j = 102
changes is reflected in original for loop
}
在第一種情況下,我想這種變化的反映。但是,「匹配」中的更改不會反映在foreach循環中。這就是問題。如何解決它?
在'for'循環,可以改變,但是你用'foreach' – Grundy
修改代碼是一個'function'在一個字符串需要(你'STR'),當你想改變'匹配「,用新字符串調用該函數。 – NoLifeKing
修改foreach集合是錯誤的,但你的代碼對我來說運行得很好。新值被分配爲匹配四次。 – nima