+ 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; }
8 Réponses
+ 2
Yes thank you
+ 1
Please specify relevant language in your question tags 👍
+ 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)
+ 1
thank you that was helpull but its not returning the whole text. it return a sentence thats it
+ 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?
+ 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();
}
0
Help!
0
yes