程序中有一数组
如:
2,3,5,3,2,5
比如我要将第二个3替换成7怎么弄呢?
因为数组里面可能有多个重复的值。现在得到了数组的位置。不知道怎么替换指定位置的值
------解决方案--------------------------------------------------------
- C# code
private int[] RepalceArray(int[] values, int pos, int oldvalue, int newvalue) { values[values.Select((a, i) => new { i, a }).Where(a => a.a.Equals(oldvalue)).Take(pos).Reverse().FirstOrDefault().i] = newvalue; return values; }
------解决方案--------------------------------------------------------
- C# code
string oldarr = "1,2,3,4,5,5,6,7,4,3"; int arrnum = 3; string newstr = "5"; string[] parts = oldarr.Split(','); parts[arrnum] = newstr; string r = string.Join(",", parts); Response.Write(r);