Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I split a string and then rejoin it?

I have the following string in C#.

"aaa,bbbb.ccc|dddd:eee"

I then split it with new char[] {',','.','|',':'}. How would I rejoin this string in the same order as before with the same characters? So the list would end up the exact same as it was before.

EXAMPLE

string s = "aaa,bbbb.ccc|dddd:eee";
string[] s2 = s.Split(new char[] {',','.','|',':'});
// now s2 = {"aaa", "bbbb", "ccc", "dddd", "eee"}
// lets assume I done some operation, and
// now s2 = {"xxx", "yyy", "zzz", "1111", "222"}

s = s2.MagicJoin(~~~~~~);  // I need this

// now s = "xxx,yyy.zzz|1111:222";

EDIT

the char[] in above sample just sample, not in same order or even will not all appear in same time in real world.

EDIT

Just a thought, how about use Regex.split, then first split by char[] get a string[], then use not the char[] to split get another string[], later just put them back. Maybe work but I do not know how to code it.

like image 958
Eric Yin Avatar asked Dec 28 '22 01:12

Eric Yin


2 Answers

It might be easier to do this with the Regex class:

input = Regex.Replace(input, @"[^,.|:]+", DoSomething);

Where DoSomething is a method or lambda that transforms the item in question, e.g.:

string DoSomething(Match m)
{
    return m.Value.ToUpper();
}

For this example the output string for "aaa,bbbb.ccc|dddd:eee" would be "AAA,BBBB.CCC|DDDD:EEE".

If you use a lambda you can very easily keep state around, like this:

int i = 0;
Console.WriteLine(Regex.Replace("aaa,bbbb.ccc|dddd:eee", @"[^,.|:]+",
    _ => (++i).ToString()));

Outputs:

1,2.3|4:5

It just depends on what kind of transformation you're doing to the items.

like image 191
porges Avatar answered Dec 30 '22 11:12

porges


Here's MagicSplit:

public IEnumerable<Tuple<string,char>> MagicSplit(string input, char[] split)
{    
    var buffer = new StringBuilder();
    foreach (var c in input)
    {
        if (split.Contains(c)) 
        {
            var result = buffer.ToString();
            buffer.Clear();
            yield return Tuple.Create(result,c);
        }
        else
        {
            buffer.Append(c);
        }
    }
    yield return Tuple.Create(buffer.ToString(),' ');
}

And two types of MagicJoin:

public string MagicJoin(IEnumerable<Tuple<string,char>> split)
{
    return split.Aggregate(new StringBuilder(), (sb, tup) => sb.Append(tup.Item1).Append(tup.Item2)).ToString();
}

public string MagicJoin(IEnumerable<string> strings, IEnumerable<char> chars)
{
    return strings.Zip(chars, (s,c) => s + c.ToString()).Aggregate(new StringBuilder(), (sb, s) => sb.Append(s)).ToString();
}

Usages:

var s = "aaa,bbbb.ccc|dddd:eee";

// simple
var split = MagicSplit(s, new char[] {',','.','|',':'}).ToArray();
var joined = MagicJoin(split);    

// if you want to change the strings
var strings = split.Select(tup => tup.Item1).ToArray();
var chars = split.Select(tup => tup.Item2).ToArray();
strings[0] = "test";
var joined = MagicJoin(strings,chars);
like image 35
yamen Avatar answered Dec 30 '22 10:12

yamen