简体   繁体   中英

Splitting a string and putting it back together

I have a string which is in a format like "C 01 ABC 02 AB"

I'm splitting it with

string[] line = String.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

and it works like a charm.

But I need to put it back in a different format.

I need first C then "01 ABC 02" and then lastly AB separately.

Basically I need to change "C 01 ABC 02 AB" into three smaller strings as "C" "01 ABC 02" and "AB".

Sadly , ABC and 02 parts can change in length to I can't assign a fixed variable to do the split.

I tried string.Join but didn't work as I wanted it to.

Seems like all other examples are joining whole array into a single string, how can I do this?

If your string is always build that first and last string parts ("C" and "AB") are without spaces, you can use something like this:

string[] parts = "C 01 ABC 02 AB".Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string part1 = parts[0]; // "C"
string part2 = string.Join(" ", parts.Skip(1).Take(parts.Length - 2)); // "01 ABC 02"
string part3 = parts[parts.Length - 1]; // "AB"

If it's always fixed 5 parts you can simplify this:

string[] parts = "C 01 ABC 02 AB".Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string part1 = parts[0]; // "C"
string part2 = string.Join(" ", parts.Skip(1).Take(3)); // "01 ABC 02"
string part3 = parts[4]; // "AB"

So, if I understand you right, you want

 "C 01 ABC 02 AB" ->             ["C", "01 ABC 02", "AB"]             // 3 items
 "C 01 ABC 02 bla bla bla AB" -> ["C", "01 ABC 02 bla bla bla", "AB"] // 3 items
 "C 01 02 AB"     ->             ["C", "01 02", "AB"]                 // 3 items

in that case

  String source = "C 01 ABC 02 AB";

  int first = source.IndexOf(' ');
  int last = source.LastIndexOf(' '); 

  String[] line = new String[] {
    source.Substring(0, first),
    source.Substring(first + 1, last - first - 1),
    source.Substring(last + 1),
  };

Depending on how varying the input data is you could also use a regular expression like ^(.+)\\s(\\d+\\s.+?\\s\\d+)\\s(.+) . This will result in a match with three groups containing the seperate pieces "C" "01 ABC 02" and "AB".

This does mean that the middle part must always exist of digits followed by a string and then followed by digits again. Added advantage is that you also check that the input string is of a certain format.

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        var result = Regex.Match("C 01 ABC 02 AB", @"^(.+)\s(\d+\s.+?\s\d+)\s(.+)");
        Console.WriteLine(result.Groups[0].Value); // "C 01 ABC 02 AB"
        Console.WriteLine(result.Groups[1].Value); // "C"
        Console.WriteLine(result.Groups[2].Value); // "01 ABC 02"
        Console.WriteLine(result.Groups[3].Value); // "AB"
    }
}

This should do it:

string x = "C 01 ABC 02 AB";
string s1 = x.Substring(0, 1);
string s2 = x.Substring(2, x.Length - 5);
string s3 = x.Substring(x.Length - 2);

Considering C and AB's length will never change.

You could use a regular expression to extract your values:

var pattern = new Regex(@"(?<first>[A-Z0-9]+)\s(?<second>(([0-9]+)\s([A-Z]+)\s[0-9]+))\s(?<third>[A-Z]+)");
var line = pattern.Match(input);
var first = line.Groups["first"].Value;
var second = line.Groups["second"].Value;
var third = line.Groups["third"].Value;

Or you could substring the chunks:

var first = line.Substring(0, line.IndexOf(' '));
var second = line.Substring(line.IndexOf(' ') + 1, line.LastIndexOf(' ') + 1);
var third = line.Substring(line.LastIndexOf(' ') + 1);

Or use Linq to select the different chunks:

string[] line = String.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var first = line.First();
var second = string.Join(' ', line.Skip(1).Take(3).ToArray());
var third = line.Last();

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