2

I need to split a string by (' ') or (''') but if in the string appears ' ' I want to keep only that space. this are examples of the string:

  1 7871.6 LIQ WCT GOR THP ' ' FIELD BHP /
  1 7871.6 'LIQ' 'WCT' 'GOR' 'THP' ' ' FIELD BHP /

and I want the output to be:

1
7871.6
LIQ
WCT
GOR
THP

FIELD
BHP
/

Any help would be appreciated

calarez
  • 55
  • 5
  • What if the string is `1 7871.6 'LIQ WCT GOR'` ? – Lasse V. Karlsen Apr 04 '21 at 21:37
  • should be 3 output: 1, 7871.6 and LIQ WCT GOR. – calarez Apr 04 '21 at 21:40
  • 1
    You can use a CSV parser, most will let you specify the delimiter (space in your case) and line ending and text qualifier which allow delimiters in an entry (often that's double quotes, but in your case you'd set it to single quote) – juharr Apr 04 '21 at 21:46

2 Answers2

1

Something like this should get he job done.. that is if you don't mind your head exploding from a regex.. p.s this should cover your weird extra case :) Oh and just so I make sure I credit the right people I got the regex for this from https://stackoverflow.com/a/51432792/5963888

var str = "1 7871.6 'LIQ' 'WCT' 'GOR' \"THP\" ' ' 'LIQ WCT GOR' FIELD BHP /";
var regex = new System.Text.RegularExpressions.Regex("([\"'])((?:\\\\1|(?:(?!\\1)).)*)(\\1)");
var match = regex.Matches(str)
    .Select(e => e.Groups)
    .Select( e => e[2].Value)
    .SelectMany( e =>
    {
        if(!(e.Trim() == String.Empty))
        {
            return e.Split();
        }
        return new[] { e };
    });
System.Console.WriteLine(String.Join(Environment.NewLine, match));
Zach Hutchins
  • 801
  • 1
  • 12
  • 16
0

Here is some code that does the trick

    string str = "1 7871.6 'LIQ' 'WCT' 'GOR' 'THP' ' ' FIELD BHP /";


    string replaceToken  = "someValueThatYouChose";
    str = str.Replace(" ' ' ", $" {replaceToken} ");
    str = str.Replace("'", "");
    string[] splitValues = str.Split(" ");
    for (int i = 0; i < splitValues.Length; i++)
    {
        if(splitValues[i] == replaceToken){
            splitValues[i] = " ";
        }
    }
    // Here is your output
    foreach (var value in splitValues)
    {
        Console.WriteLine(value);
    }
Ntjs95
  • 128
  • 1
  • 8
  • This would not work on the example in the comments of `1 7871.6 'LIQ WCT GOR'` where the results should be "1", "7871.6" and "LIQ WCT GOR". Even with the original example I'm pretty sure they expect a single space and not an empty string for `' '` – juharr Apr 04 '21 at 22:18
  • Its Ok because in theory that example never should happen, your solution works fine .. Thank You very much – calarez Apr 04 '21 at 22:21
  • @calarez You'll likely want to update that to `splitValue[i] = " ";` so you'll get the single space instead of an empty string as it looks like your expected results did include a single space. – juharr Apr 04 '21 at 22:22
  • in this case it doesn't matter if it is single space or empty string, I just need any value so the total number of words counts will be always the same (count=10) – calarez Apr 04 '21 at 22:30