替换字符串数组中所有出现的字符串 [英] Replace all occurences of a string from a string array
本文介绍了替换字符串数组中所有出现的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串数组,如:
I have a string array like:
string [] items = {"one","two","three","one","two","one"};
我想一次用零替换所有的.那么项目应该是:
I would like to replace all ones with zero at once. Then items should be:
{"zero","two","three","zero","two","zero"};
我找到了一个解决方案 我如何替换字符串数组中的项目?.
I found one solution How do I replace an item in a string array?.
但它只会替换第一次出现.替换所有出现的最佳方法/方法是什么?
But it will replace the first occurrence only. Which is the best method/approach to replace all occurrences?
推荐答案
string [] items = {"one","two","three","one","two","one"};
items = items.Select(s => s!= "one" ? s : "zero").ToArray();
从 这里找到答案.
这篇关于替换字符串数组中所有出现的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文