简体   繁体   中英

Add comma in string if boolean is false

I have 3 booleans:

Boat, Plane, Car

And 3 strings:

ReloadBoat, ReloadPlane, ReloadCar

Depending on these booleans, if false, I need to add commas between the strings.

string errorMessage = (Boat? "" : " " + ReloadBoat) + (Plane? "" : (errMessage) + ReloadPlane) + (Car? "" : ", " + ReloadCar);

For the above, the issue I am getting is, if both Boat and Plane are true, I am getting the errorMessage as ", ReloadCar".

I want it to be "ReloadCar" only.

Any idea on how to do this?

Breaking it out makes the code more readable and maintainable. It'll get really hard work to use your bools in the manner you have there. Instead I recommend you put the commas on your error messages as a matter of routine. Concatenate them together and remove any trailing commas:

string err="";

if(boatNeedsReload)
  err+="ReloadBoatErrorMessageWithComma, ";
if(planeNeedsReload)
  err+="ReloadPlaneErrorMessageWithComma, ";
if(carNeedsReload)
  err+="ReloadCarErrorMessageWithoutComma";

err = err.TrimEnd(new[]{' ',','});

So the method is:

  • Put punctuation between all your elements regardless
  • Trim off trailing redundant punctuation as the last operation
  • I didn't put any punctuation after ReloadCar because as the last item, it isn't strictly necessary. If this were extended in future to add another item at the end you'd have to remember to punctuate ReloadCar. You might thus wish to consider punctuating car right now and not have to remember to do it next time

If you use a stringbuilder you can interrogate the length and knock 2 off it if needs be:

StringBuilder err=new StringBuilder();

if(boat)
  err.Append("ReloadBoat, ");
if(plane)
  err.Append("ReloadPlane, ");
if(car)
  err.Append("ReloadCar, ");
if(err.Length>0);
  err.Length-=2;
  • this time I did punctuate reloadcar because this method wouldn't work properly without it

Don't try to do too much on one line in your code; you'll reach a point where it needs modifying in a months time and it'll take longer to work out how it works and how to extend it than just breaking it out to something readable and hence maintainable

As an example, this does the same as the first code block, but it's a bit "what the..?"

string err = (
  (boatNeedsReload ? "ReloadBoatErrorMessageWithComma, ":"")+
  (planeNeedsReload ? "ReloadPlaneErrorMessageWithComma, ":"")+
  (carNeedsReload ? "ReloadCarErrorMessageWithoutComma":""))
  .TrimEnd(new[]{' ',','});

Falco makes a good point, that you should strive to make your Boolean variables have a name that declares a truth, like "isTooYoung" or "boatNeedsReload". Make your Boolean have a positive spirit, as it starts to get confusing if you write if(boatDoesntNeedReload==false) . Note also that classic advice is to not compare a Boolean with another Boolean to realise a Boolean, but consider that comparing with false can make code more readable than using ! to invert a truth

I would create a list of strings with errors and appended to it based on the bools and string join them with a comma or whatever you want.

        var boatErrorMessage = "boatErrorMessage";
        var planeErrorMessage = "planeErrorMessage ";
        var carErrorMessage = "carErrorMessage";

        var isBoat = true;
        var isPlane = false;
        var isCar = true;

        var errorMessageList = new List<string>();

        if (isBoat)
            errorMessageList.Add(boatErrorMessage);

        if (isPlane)
            errorMessageList.Add(planeErrorMessage);

        if (isCar)
            errorMessageList.Add(carErrorMessage);

        Console.WriteLine(string.Join(", ", errorMessageList));

Output: boatErrorMessage, carErrorMessage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM