remove-method

.NET - Remove from a List<T> within a 'foreach' loop

主宰稳场 提交于 2020-01-19 06:45:46
问题 I have code that I want to look like this: List<Type> Os; ... foreach (Type o in Os) if (o.cond) return; // Quitting early is important for my case! else Os.Remove(o); ... // Other code This doesn't work, because you cannot remove from the list when you are inside a foreach loop over that list: Is there a common way to solve the problem? I can switch to a different type if needed. Option 2: List<Type> Os; ... while (Os.Count != 0) if (Os[0].cond) return; else Os.RemoveAt(0); ... // Other code

How do I remove the last vowel in a string in Ruby?

邮差的信 提交于 2019-12-11 02:13:06
问题 How can I define the -last vowel- in a string? For example, I have a word "classic" I want to find that the last vowel of the word "classs i c" is the letter " i ", and delete that last vowel. I'm thinking : def vowel(str) result = "" new = str.split(" ") i = new.length - 1 while i < new.length if new[i] == "aeiou" new[i].gsub(/aeiou/," ") elsif new[i] != "aeiou" i = -= 1 end end return result end 回答1: r = / .* # match zero or more of any character, greedily \K # discard everything matched so

.NET - Remove from a List<T> within a 'foreach' loop

眉间皱痕 提交于 2019-11-28 04:05:42
I have code that I want to look like this: List<Type> Os; ... foreach (Type o in Os) if (o.cond) return; // Quitting early is important for my case! else Os.Remove(o); ... // Other code This doesn't work, because you cannot remove from the list when you are inside a foreach loop over that list: Is there a common way to solve the problem? I can switch to a different type if needed. Option 2: List<Type> Os; ... while (Os.Count != 0) if (Os[0].cond) return; else Os.RemoveAt(0); ... // Other code Ugly, but it should work. Do you really need to do this within a foreach loop? This will achieve the