-3

Using the below code I see the result as

cardetails =  "<ul>" + car.model.Product.Replace("•", "<li>").Replace("\n", "</li>") + "</li></ul>";

Strng 1: "Product":"• Hatchback\n• Hyundai"

  • Hatchback
  • Hyundai

In the same string I have added Sedan as new type now the complete string is "Product":"• Hatchback\n• Sedan\n• Hyundai" here I need to skip Hatchback and retrieve only Sedan in First list item using conditional operator

  • Sedan
  • Hyundai
 cardetails = carType == CarTypes.Hatchback.ToString()
            ? "<ul>" + car.model.Product.Replace("•", "<li>").Replace("\n", "</li>") + "</li></ul>"
            : "How to add logic here to retrieve Sedan in first <li> and skip hatchback";

How can I achieve this

  • I don't see any LINQ here. Is this code your delegate within a LINQ call e.g. `cars.Select(c => c.CarType == CarType.Hatchback ? "" : ... )` ? Don't name enums in the plural unless they're `[Flags]`, by the way – Caius Jard Feb 16 '21 at 20:56
  • 1
    Non-understandable question :| – Amir Feb 16 '21 at 21:01
  • Trying to manipulate strings like this is only going to make things harder for you in the long run. My suggestion: 1. Get your `car.Model.Product` value into a format that's easier to work with (e.g. Manufacturer > Shapes). 2. Generate whatever HTML you need to from that. – ProgrammingLlama Feb 17 '21 at 06:12
  • 1
    Can I ask why you are building a string like that that contains HTML tags? Usually doing that is a "very bad idea", hence my question. – mjwills Feb 17 '21 at 06:44

1 Answers1

0

I think you are confusing the behaviour of Skip(). I assume that car.model.Product is a string, which means Skip will treat the string as an IEnumerable<char> and therefore Skip(1) will skip one character in the string:

var myString = "this is a string";
var result = myString.Skip(1);
// result == "his is a string"

One way you could solve this is by splitting the string into the parts you are interested in and then skip the first part you want to discard:

var productParts = car.model.Product
    // We don't really care about the line breaks
    .Replace(@"\n", "")
    // Split the string and remove any empty parts and
    // remove (trim) spaces from the ends of each part
    .Split("•", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntriesTrimEntries)
    // Wrap each part in a <li> tag
    .Select(part => $"<li>{part}</li>");

cardetails = carType == CarTypes.Hatchback.ToString()
    ? $"<ul>{string.Join(Environment.NewLine, productParts)}</ul>"
    : $"<ul>{string.Join(Environment.NewLine, productParts.Skip(1))}</ul>";
// Notice: we skip the first (Hatchback) part above
Xerillio
  • 4,855
  • 1
  • 17
  • 28
  • I have a list with three values
    • Hatchback
    • Hatchback
    • Hyundai
    – Sayeed Ahmed Feb 17 '21 at 11:50
  • @SayeedAhmed Do you mean something is wrong? – Xerillio Feb 17 '21 at 16:04
  • @ Xerillio At present I can successfully display Sedan values but not for Hatchback type Below is final string I get
  • Hatchback
  • Sedan
  • Hyundai
  • , Whenever i get Car model as Hatchback I need to remove
  • Sedan
  • from the list which is not happening using skip(2) and I tried with RemoveAt property which throws compile error as Void cannot convert to String, Appreciate your help,! – Sayeed Ahmed Feb 17 '21 at 17:39
  • @ Xerillio Code I used - car.ModelType == CarModels.Sedan.ToString() ? $"{string.Join(Environment.NewLine, productParts.Skip(1))}" : $"{string.Join(Environment.NewLine, productParts.RemoveAt(2))};// RemoveAt throws compilation error , If I use Skip(2) to remove Middle element from string It's removing top 2 – Sayeed Ahmed Feb 17 '21 at 17:47
  • @SayeedAhmed Yea, you can't do that with standard LINQ. The easiest would be to turn it into a list first (`ToList()`) and then remove it like you've tried. But if you have multiple conditions/checks like this you should consider something smarter than relying on the values being at specific indexes in the list. – Xerillio Feb 17 '21 at 20:12
  • I have achieved it with two lines of code, is there any way I can improve the code with one single statement var sedanTxt= car.model.Product.Replace("•", "
  • ").Split('\n').Select(part => $"{part}").Where((x, y) => y % 2 == 0).ToList(); var hatchBackTxt= car.model.Product.Replace("•", "
  • ").Split('\n').Select(part => $"{part}").ToList(); descriptionText = car.Model == Models.hatchback ? $"{string.Join(Environment.NewLine, hatchBackTxt.Skip(1))}" : $"{string.Join(Environment.NewLine, sedanTxt)}";
  • – Sayeed Ahmed Feb 21 '21 at 07:58
  • @SayeedAhmed this code is very fragile. The smallest changes in the input will break you code. You need to think about parsing the input in a way so it's not dependable on the number of elements and line breaks etc. One-liner code snippets are not a sign of good coding. You should focus on making it readable. – Xerillio Feb 21 '21 at 09:53
  • Can you pls check my post so far I haven't got any answers yet posted on (13/6/2021) if you have any answers for this.... https://stackoverflow.com/questions/67961086/api-abuse-security-vulnerability-issue-mvc-app – Sayeed Ahmed Jun 14 '21 at 14:22