Jobert Enamno
Jobert Enamno

Reputation: 4461

Split String by Two Characters Ignore Whitespace Before, In Between or After any of the Characters

I have an input string for example

var input = "'Test string','abc'";

I want to split it by exactly two characters that is ', ignoring white spaces before, in between or after the delimiters. For example based on the input I'm expecting the following output.

index0='Test string'
index1='abc'

I want to retain that output even if my input looks like any of these.

var input = "'Test string','abc'";
var input = "'Test string ','abc'";
var input = "'Test string' ,'abc'";
var input = "'Test string', 'abc'";
var input = "'Test string   '   ,   'abc'";

As long as there are two characters ', (single quote and comma) regardless that they have spaces before, in between or after, I want to split a string based on those two char delimiters. Splitting should only by two chars. If an input has only ' or , retain the input as is. Only split the input by ', not by any of the two chars. Please help. Here's my code.

   static void Main(string[] args)
    {
        var input = "'Test string','abc'";
        var results = Regex.Split(input, @"[ ',]+");
        foreach (var item in results)
            Console.WriteLine(item);
        Console.ReadLine();

    }

Upvotes: 0

Views: 1231

Answers (5)

Raka
Raka

Reputation: 427

You can create method like below to get desired value. Call this method for each data.

private string GetValues(string input)
{
    StringBuilder newValue = new StringBuilder();
    string[] parts = input.Split(',');
    int counter = 0;
    foreach (string str in parts)
    {
        newValue.AppendLine("index" + counter++ + ":" + "'" + str.Trim().Trim('\'').Trim() + "'");

    }
    return newValue.ToString();
}

Input:

"'Test string','abc'";
"'Test string ','abc'";
"'Test string' ,'abc'";
"'Test string', 'abc'";
"'Test string   '   ,   'abc'";
"Test string','abc','John's test'";

Output is as below:

index0:'Test string'
index1:'abc'

index0:'Test string'
index1:'abc'

index0:'Test string'
index1:'abc'

index0:'Test string'
index1:'abc'

index0:'Test string'
index1:'abc'

index0:'Test string'
index1:'abc'
index2:'John's test'

Upvotes: 0

Rizki Fitra
Rizki Fitra

Reputation: 31

Try this:

string input = "'   Test string  ','abc'";
        string[] listInput = input.Split(',');
        foreach (string li in listInput){
          String output = li.TrimStart('\'').TrimEnd('\'').Trim();
            output = "'"+output+"'";
          Console.WriteLine(output);
        }

Upvotes: 1

Fabio
Fabio

Reputation: 32443

Split by comma and trim single quote and spaces afterwards

var input = "'Test string   '   ,   'abc', 'John's test'";

var output = input.Split(',').Select(p => p.Trim().Trim('\'').Trim());

// Test string, abc, John's test.

Upvotes: 1

Prime
Prime

Reputation: 2482

If the input is predictable and never contains an escaped quotation mark, you can do this:

input.Split('\'').Where((x, i) => i % 2 == 1).Select(p => p.Trim());

See it in use here: https://dotnetfiddle.net/AqookJ


input.Split('\'')

Splits the input by '

.Where((x, i) => i % 2 == 1)

Selects every second item of the split, which is everything inside of quotes in this context. For efficiency this can be changed to (x, i) => (i & 1) == 1

.Select(p => p.Trim())

Will remove whitespace from the beginning and end of every item in the collection.


If however there are quotation marks inside of the target strings you can use this following method:

public static IEnumerable<string> Split(string inpt)
{
    IEnumerable<string> res = Regex.Split(inpt, @"('\s*,\s*'|^\s*'|'\s*$)").Where((x, i) => i % 2 == 0).Select(p => p.Trim()).Skip(1);
    return res.Take(res.Count() - 1);
}

This will split the input by '\s*,\s*', and also by ^\s*' and '\s*$.

It will then discard the first and last items of the collection, which will be the first and last quotation marks.

See it in action here: https://dotnetfiddle.net/sQ6Y2x

Upvotes: 0

TheGeneral
TheGeneral

Reputation: 81583

Or regex with this pattern for optional white space (?<=')\s*,\s*

Example

var inputs = new []{ "'Test string','abc'", 
                     "'Test string ','abc'", 
                     "'Test string' ,'abc'", 
                     "'Test string', 'abc'", 
                     "'Test string   '   ,   'abc'", 
                     "Test string','abc','John's test'" };

foreach (var input in inputs)
{
   var results = Regex.Split(input, @"(?<=')\s*,\s*");
   foreach (var result in results)
      Console.WriteLine(result);
}

Output

'Test string'
'abc'
'Test string '
'abc'
'Test string'
'abc'
'Test string'
'abc'
'Test string   '
'abc'
Test string'
'abc'
'John's test'

Visualized

enter image description here

Full Demo here

Upvotes: 1

Related Questions