Link to home
Start Free TrialLog in
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

Avatar of ste5an
ste5an
Flag of Germany image

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?
Avatar of Skale
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();
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".
Avatar of 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
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@saige, that's exactly the reason why I asked for the logic.. Cause yours implements #1 whereas the original code implements #3.
@ste5an, understood.  I thought about trying to comment additionally pointing out what you were stating, but your comments were pretty clear to me.  ;)

-saige-