- Home /
Remove String Line If Contains Specific Word C#
Hey, I am working on a game launcher (BY THE WAY IN C#). I have a .txt file stored on a database and unity gets the address of where the .txt file is stored and reads it, then converts it into a string. What I need help with is trying to find in the string, a word (lets call it) 'Old Version', and then remove that entire line where ever the word 'Old Version' appears.
I'm having issues with using myString.Contains(parameters & parameters), & myString.Replace(parameters, parameters)
because I can't seem to delete the rest of the line, only the "word" which is in the 'Contains'. Any help is much appreciated! - Trentos El Bartos!
Answer by jgodfrey · Apr 20, 2016 at 10:24 PM
Well, you don't really show how you've loaded the file, but it'll be easier to remove a "line" of data if you store the lines individually. I'd probably do it something like this:
string searchFor = "Old Version";
string[] lines = File.ReadAllLines(@"c:\yourfile.txt");
for (int i = lines.Count - 1; i >= 0; i--)
{
if (lines[i].Contains(searchFor) { lines.RemoveAt[i]; }
}
Now, "lines" will contain your loaded lines (in an array) minus the line(s) that contain the search string.
It's not clear whether you want to store the modified file, or just have a modified string "in memory".
To write the modified array to a file, just do:
File.WriteAllLines(@"c:\your_new_file.txt", lines);
To create an in-memory string with the array, you want something like:
string content = string.Join(" ", lines);
Or, maybe...
string content = string.Join("\r\n", lines);
Depending on what format you want...
Caution - all code typed directly into the forum and untested...
@jgodfrey Thank you for your fast reply! I'm having issues trying to use the code samples which you have provided. I'll explain to you the loading of the string. So, I have a string which is this: string loadURL = "http://trentbartaphotography.com/humans.txt";
. Then I have a Coroutine which runs which is: StartCoroutine (downloadText (loadURL));
. This then goes through the Coroutine called 'downloadText' with the loadURL string.
Now my main string which contains all of the data from the .txt file is: string text;
. Then in the Coroutine downloadText, this line of code converts all of the content in .txt file to the string and stores it in the string text
with the line of code: text = www.text.ToString();
.
So what i'm trying to do, is check the string text;
for the word "Old Version", then remove that entire line from the string. I've managed to remove the entire first line of the string by using: int index = www.text.IndexOf (System.Environment.NewLine);
(JUST CLARIFYING 'www.text' is not the same as the string 'text'), then text = www.text.Substring (index + System.Environment.NewLine.Length);
. But I just can't seem to remove the line which contains "Old Version" in it...
So, it seems that you have a single string variable that contains a bunch of text where "lines" are delimited by System.Environment.NewLine, right? And, you want to remove the line(s) containing some search string. O$$anonymous$$, how about something like:
string searchFor = "Old Version";
// split the content of the text variable into lines
var lines = text.Split(new string[] { System.Environment.NewLine } StringSplitOptions.None);
// Iterate through the lines and remove any containing the search string
for (int i = lines.Count - 1; i >= 0; i--)
{
if (lines[i].Contains(searchFor) { lines.RemoveAt[i]; }
}
// Join the remaining lines back into a single string
string textCleaned = string.Join(System.Environment.NewLine, lines);
Hey, thanks again, but I can't get the line var lines = text.Split(new string[] { System.Environment.NewLine } StringSplitOptions.None);
to work. It seems that i can't do the new string[]
and then the rest of it, because its just throwing errors like 'Unexpected Symbol: StringSplitOptions'
O$$anonymous$$, sorry - that code was typed directly into the forum and had a few typos. I've fixed the problems, and tested it this time... ;^)
string searchFor = "Old Version";
// split the content of the text variable into lines
var lines = text.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None).ToList();
// Iterate through the lines and remove any containing the search string
for (int i = lines.Count - 1; i >= 0; i--)
{
if (lines[i].Contains(searchFor)) { lines.RemoveAt(i); }
}
// Join the remaining lines back into a single string
string textCleaned = string.Join(System.Environment.NewLine, lines);
Your answer
Follow this Question
Related Questions
Localization via script works in editor but not in build, why? 1 Answer
how loop through char in a string to find tags 0 Answers
Convert Script To String - Not your average typecasting 2 Answers
Create a seed or string to run multiple methods. C# 3 Answers
Does Unity's Mono do a reference compare in its string == operator? 2 Answers