Replace a item in string array [closed]
I have a string array and want to replace a value from that array.
Example:
string[] stud = new[] {"1","12","Mark","M"};
string[] otherStud = new [] {"2","16","MarkMark","F"};
I want to replace the Mark
with Tom
, then Result should be
Result:
string[] stud = new [] {"1","12","Tom","M"};
string[] otherStud = new [] {"2","16","TomTom","F"};
please suggest any solutions.
Thanks
stud = stud.Select( s => s.Replace("Mark","Tom") ).ToArray();
string[] stud = {"1", "12", "Mark", "M"};
for (int i = 0; i < stud.Count(); i++)
{
stud[i] = stud[i].Replace("Mark", "Tom");
}
string[] stud = { "1", "12", "Mark", "M", "2", "16", "MarkMark", "F" };
for (int i = 0; i < stud.Length; ++i)
stud[i] = stud[i].Replace("Mark", "Tom");
Use for
and in loop use String.Replace
. You should be able to figure out how it should look exacly. :)
you can traverse the string array using foreach and can replace the required string
stud.Select(x => x != "Mark" ? x : "Tom");
精彩评论