简体   繁体   English

将字符串拆分为两个字符,忽略任何字符之前,之中或之后的空格

[英]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();

    }

Or regex with this pattern for optional white space (?<=')\\s*,\\s* 或带有此模式的正则表达式可用于可选的空格(?<=')\\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 可视化

在此处输入图片说明

Full Demo here 完整的演示在这里

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

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.

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 看到它在这里使用: 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 为了提高效率,可以将其更改为(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*$ . 这将用'\\s*,\\s*'以及^\\s*''\\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 在此处查看其运行情况: https : //dotnetfiddle.net/sQ6Y2x

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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