+ 1

How find if a sting contain in a text file and then ToUpper the string

I'm trying to return the whole file with Upper case the string being search( or contains in the text file ). My program is only returning one string contains in the file. public string containWord(string searchFor) { string result = ""; var lines = File.ReadAllLines("invictus.txt "); foreach (var line in lines) { if (line.Contains(searchFor)) { var text = searchFor.ToUpper(); result = text.Trim(); } } return result; }

3rd Mar 2020, 2:40 AM
Microne mafika
Microne mafika - avatar
8 odpowiedzi
+ 2
Yes thank you
3rd Mar 2020, 3:48 AM
Microne mafika
Microne mafika - avatar
+ 1
Please specify relevant language in your question tags 👍
3rd Mar 2020, 3:08 AM
Ipang
+ 1
Microne mafika Firstly, Thanks for adding language specifics 👍 I haven't tested with actual file content. But I guess `Replace` method might be what you need for this purpose. * Before the `foreach` loop, add: string replacement = searchFor.ToUpper(); string result = ""; * Inside the conditional `if` add: result = line.Replace(searchFor, replacement).Trim(); break; // exit loop when found (Edited)
3rd Mar 2020, 3:32 AM
Ipang
+ 1
thank you that was helpull but its not returning the whole text. it return a sentence thats it
3rd Mar 2020, 3:43 AM
Microne mafika
Microne mafika - avatar
+ 1
Sorry I misunderstood your intention. So you want to replace the whole string (read from file) with every search string being converted to uppercase? or do you want to return just the search string converted to uppercase when it is found in one of the lines?
3rd Mar 2020, 3:47 AM
Ipang
+ 1
To return the whole string, you can try this (untested): if(lines.Contains(searchFor)) { string replacement = searchFor.ToUpper(); string result = lines.Replace(searchFor, replacement); return result.Trim(); } else { return lines.Trim(); }
3rd Mar 2020, 3:54 AM
Ipang
0
Help!
3rd Mar 2020, 2:50 AM
Microne mafika
Microne mafika - avatar
0
yes
3rd Mar 2020, 3:48 AM
Microne mafika
Microne mafika - avatar