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?
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;
}
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.IndexO f(parseFul lName, parseFullName.Last())]).To Array();
parseFullName = parseFullName.Where(w => w != parseFullName[Array.IndexO f(parseFul lName, parseFullName.Last())]).To Array();
parseFullName = parseFullName.Where(w => w != parseFullName[Array.IndexO
parseFullName = parseFullName.Where(w => w != parseFullName[Array.IndexO
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
The keyword is "all occurances".
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@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-
-saige-
The code splits a string and then removes all occurences of the first and last two items. E.g.
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. usingOpen 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?