Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TBART82 · Apr 20, 2016 at 09:30 PM · scripting problemstringstrings

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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...

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TBART82 · Apr 20, 2016 at 11:08 PM 0
Share

@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 textwith 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...

avatar image jgodfrey · Apr 20, 2016 at 11:18 PM 0
Share

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


avatar image TBART82 jgodfrey · Apr 20, 2016 at 11:40 PM 0
Share

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'

avatar image jgodfrey TBART82 · Apr 21, 2016 at 12:25 AM 0
Share

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);
Show more comments
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges