简体   繁体   中英

Find and move unknown word between particular characters in the string C#

I'm trying to find and move unknown word between particular characters in the string in C#. Example:

// this is a string from the file
begining of the string - "    TASK PERS partdata pd_Test_05:=["Call_Test_05","Test_05","T_ROB1",1,0,"",""];" - end of the string.
// I insert that string inside the string[] lines.
// I need to found and seperate word "Test_05" from that string.

Its hard to know how to give an answer, we dont know what makes Test_05 the particular target.

As a general guide you can either use Regular expressions if the target string matches a pattern. You might find this site useful http://regexstorm.net/

Or you can use string operations like IndexOf (search for a given string in a string), Substring (slice out a piece of a string), Replace,...

It looks like you might be able to do something like this:

List<string> test = yourString.Split(',').ToList<string>();

string finalProduct = test[1];

At this point, your string would look like "Test_05" . Just replace the quotation marks with C#'s replace or Regex replace them.

Thanks GMR516. I finished with 'Trim.

        string[] lines = File.ReadAllLines("testFile.mod");
        List<string> test = lines[0].Split(',').ToList<string>();

        string finalProduct = test[1].Remove(1, 0);
        Console.WriteLine($"Before trim: {finalProduct}");

        char[] charsToTrim = { '*', ' ', '\'', '"' };
        // Trim method can remove any characters from specified string
        string result = finalProduct.Trim(charsToTrim);

        Console.WriteLine($"After Trim: {result}");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM