开发者

Replace a item in string array [closed]

开发者 https://www.devze.com 2023-03-14 12:48 出处:网络
开发者_高级运维 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form.
开发者_高级运维 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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");

0

精彩评论

暂无评论...
验证码 换一张
取 消