tRuEsAtM
tRuEsAtM

Reputation: 3668

C# split string on a separator then remove an element and then join again by using a separator

I have a string like "SS\u001ffsdf\u001fA123456\u001f".

I am using below code to split the string,

var record = _recordSection.Split("\u001f");

I get a string array-like "SS", "ffsdf", and "A123456".

I want to delete "A123456" and then again format the string like

"SS\u001ffsdf\u001f\u001f" How should I go about this?

Upvotes: 0

Views: 1789

Answers (5)

Nicholas Carey
Nicholas Carey

Reputation: 74197

Doesn't need to be more complicated than:

using System;
using System.Linq;

namespace HandyExtensions
{
    static class StringExtensions
    {
        static string Splice(this string s, string separator, Func<string, int, bool> desired ) {
            string[] segments = s.Split(separator)
                                 .Where( desired )
                                 .ToArray()
                                 ;
            return String.Join(separator, segments);
        }
    }
}

That should let you say things like

"a,b,c,d,e,f,g".Splice(",", (s,i) => i % 2 == 0 )

and get back "a,c,e,g"

Upvotes: 0

Flydog57
Flydog57

Reputation: 7111

Try this:

private const string InitialString = "SS\u001ffsdf\u001fA123456\u001f";
private const char Separator = '\u001f';

and then just split, replace the third item and join things back:

var splitStrings = InitialString.Split(Separator);
splitStrings[2] = string.Empty;
var result = string.Join(Separator.ToString(), splitStrings);

When finished, result looks like (copied from debugger):

"SS\u001ffsdf\u001f\u001f"

If you want "the last item" instead of the third, use splitStrings.Length-1 as the index instead of 2.

Upvotes: 0

Charlieface
Charlieface

Reputation: 71308

All nice solutions, but using string.Split can be inefficient on large strings.

If you need the last element to be removed, you can calculate the substring size by doing this:

var record = _recordSection.Substring(
    0,
    _recordSection.LastIndexOf('\u001f', _recordSection.Length - 2)) + "\u001f\u001f";

The same can be done going from the beginning with consecutive IndexOf calls.

No-one seems to have pointed out that \u001f is a single escaped char.

Upvotes: 0

Nigrimmist
Nigrimmist

Reputation: 12338

If you said, you need to delete third element - you can use xanato's solution or this :

int positionToRemove = 3;
string separator = "\u001f";
string _recordSection = "SS\u001ffsdf\u001fA123456\u001f";

var recordList = _recordSection.Split(separator).ToList();
recordList.Remove(recordList[positionToRemove - 1]);
var result = string.Join(separator, recordList);

Console.WriteLine(result); //will output "SS\u001ffsdf\u001f\u001f"

Upvotes: 0

xanatos
xanatos

Reputation: 111840

If you want to remove the element A123456, a .Select() and a ternary operator will be enough.

string _recordSection = "SS\u001ffsdf\u001fA123456\u001f";
var record = _recordSection.Split('\u001f');
var merged = string.Join("\u001f", record.Select(x => x != "A123456" ? x : string.Empty));

If you want to remove the third element it is even easier:

string _recordSection = "SS\u001ffsdf\u001fA123456\u001f";
var record = _recordSection.Split('\u001f');
record[2] = string.Empty;
var merged = string.Join("\u001f", record);

Upvotes: 2

Related Questions