Avatar of Skale
Skale
 asked on

How to generate a string in my code with more easier way in C#

Hello I'm using below code;

It's splitting according to underscores at the end it removes first and last two items. Then generates tat string again.

Code seems a bit weird to me. Does anyone know are there any more efficient way?

        public static string GetCutForceChannelBaseName(string fullname)
        {
            string[] parseFullName = fullname.Split(new string[] { "_" }, System.StringSplitOptions.None);
            parseFullName = parseFullName.Where(w => w != parseFullName[System.Array.IndexOf(parseFullName, parseFullName.First())]).ToArray();
            parseFullName = parseFullName.Where(w => w != parseFullName[System.Array.IndexOf(parseFullName, parseFullName.Last())]).ToArray();
            parseFullName = parseFullName.Where(w => w != parseFullName[System.Array.IndexOf(parseFullName, parseFullName.Last())]).ToArray();
            string generateName = System.String.Join("_", parseFullName);
            return generateName;
        }

Open in new window

C#

Avatar of undefined
Last Comment
it_saige

8/22/2022 - Mon
ste5an

It's splitting according to underscores at the end it removes first and last two items.
Well, while this is a correct describtion, its not precise enough.

The code splits a string and then removes all occurences of the first and last two items. E.g.

namespace ConsoleCS
{
    using System;
    using System.Linq;

    public class Program
    {
        public static void Main(string[] args)
        {
            string test = "a_b_c_d_e_a_b_c";
            Console.WriteLine(GetCutForceChannelBaseName(test));

            Console.WriteLine("\nDone.");
            Console.ReadLine();
        }

        public static string GetCutForceChannelBaseName(string fullname)
        {
            string[] parseFullName = fullname.Split(new string[] { "_" }, StringSplitOptions.None);
            parseFullName = parseFullName.Where(w => w != parseFullName[Array.IndexOf(parseFullName, parseFullName.First())]).ToArray();
            parseFullName = parseFullName.Where(w => w != parseFullName[Array.IndexOf(parseFullName, parseFullName.Last())]).ToArray();
            parseFullName = parseFullName.Where(w => w != parseFullName[Array.IndexOf(parseFullName, parseFullName.Last())]).ToArray();
            string generateName = string.Join("_", parseFullName);
            return generateName;
        }
    }
}

Open in new window

returns d_e. But as First and Last are defined on the mutating intermediate result, this means especially Last is not a fixed value. E.g. using

string test = "a_b_c_d_e_a_b_c_a";

Open in new window

returns also d_e.

Thus the question is imho wrong. It's not about efficiency. The question is:

What is the correct logic?
Skale

ASKER
Yes for removing two last items first i remove last and then read that one and again remove last one so at the end i can be able to remove last two one. Yes you're right it's more about logic.
 
parseFullName = parseFullName.Where(w => w != parseFullName[Array.IndexOf(parseFullName, parseFullName.Last())]).ToArray();
parseFullName = parseFullName.Where(w => w != parseFullName[Array.IndexOf(parseFullName, parseFullName.Last())]).ToArray();
ste5an

Please confirm that you want the following output for the given samples:

a_b_c_d_e_a_b_c             => d_e
a_b_c_d_e_a_b_c_a           => d_e
a_b_c_d_e_a_b_c_a_a_a_a_a   => d_e
a_b_c_d_e_a_aaaaa_b_c_aaaaa => b_d_e_b
a_b_c_d_e_f_a_b             => c_d_e

Open in new window

The keyword is "all occurances".
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Skale

ASKER
Hi ste5an,

it's currently doing that:

string test = "a_b_c_d_e_f_g_h";

array = a, b, c, d, e, f, g, h

//Remove first
a, b, c, d, e, f, g, h => b, c, d, e, f, g, h

// Remove last
b, c, d, e, f, g, h  => b, c, d, e, f, g

// Again remove last
b, c, d, e, f, g => b, c, d, e, f


Join with "_"
b_c_d_e_f

and b_c_d_e_f is my target string.
SOLUTION
ste5an

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
it_saige

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ste5an

@saige, that's exactly the reason why I asked for the logic.. Cause yours implements #1 whereas the original code implements #3.
it_saige

@ste5an, understood.  I thought about trying to comment additionally pointing out what you were stating, but your comments were pretty clear to me.  ;)

-saige-
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.