We help IT Professionals succeed at work.
Get Started

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

Skale asked
Last Modified: 2019-12-27
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

Watch Question
Distinguished Expert 2019
This problem has been solved!
Unlock 2 Answers and 8 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE