user1461166
user1461166

Reputation: 77

How to delete words between specified chars?

I want to create a method that reads from every line from a file. Next, it has to check between the pipes and determine if there are words that are more than three characters long, and are only numbers. In the file are strings organized like this:

What's going on {noway|that's cool|1293328|why|don't know|see}

With this sentence, the software should remove 1293328.

The resulting sentence would be:

What's going on {noway|that's cool|don't know}

Until now I am reading every line from the file and I made the functions that determine if the words between | | have to be deleted or not (checking a string like noway,that's cool, etc)

I don't know how to get the strings between the pipes.

Upvotes: 2

Views: 166

Answers (3)

Vinoth
Vinoth

Reputation: 1

        string YourStringVariable = "{noway|that's cool|1293328|why|don't know|see}";
        string[] SplitValue=g.Split('|');
        string FinalValue = string.Empty;
        for (int i = 0; i < SplitValue.Length; i++)
        {
            if (!SplitValue[i].ToString().Any(char.IsDigit))
            {
                FinalValue += SplitValue[i]+"|";    

            }

        }

Upvotes: 0

Omar
Omar

Reputation: 16621

What's about:

string RemoveValues(string sentence, string[] values){

   foreach(string s in values){
      while(sentence.IndexOf("|" + s) != -1 && sentence.IndexOf("|" + s) != 0){
         sentence = sentence.Remove(sentence.IndexOf("|" + s), s.Lenght + 1);
      }
   }

   return sentence;
}

In your case:

string[] values = new string[3]{ "1293328", "why", "see" };
string sentence = RemoveValues("noway|that's cool|1293328|why|don't know|see", values);
//result: noway|that's cool|don't know

Upvotes: 0

Tom
Tom

Reputation: 1360

You can split a string by a character using the Split method.

string YourStringVariable = "{noway|that's cool|1293328|why|don't know|see}";

YourStringVariable.Split('|');  //Returns an array of the strings between the brackets

Upvotes: 1

Related Questions