0

I am new to this comunity and I am seeking advise on how to improve my current script. Below is the code:

if (condition1 == true) string stringname  = "dog";
if (condition2 == true) string stringname1 = "cat";
if (condition3 == true) string stringname2 = "mouse";
if (condition4 == true) string stringname3 = "crab";

Format.String("Animal Type: {0}, {1}, {2}, {3}", stringname, stringname1, stringname2, stringname3); // print to output

Specifically what I would like is to be able to display the result in the output window in the following way:

example 1: assuming condition 1 and 3 are true and 2 and 4 are false: "Animal Type: dog, mouse" Whilst with my current script I would get: "Animal Type: dog,, mouse,"

example 2: assuming condition 2 and 3 are true: "Animal Type: cat, mouse" Whilst with my current script I would get: "Animal Type: ,cat, mouse,"

6 Answers 6

3
var animals = new List<string>();
if (condition1) animals.Add("dog");
if (condition2) animals.Add("cat");
if (condition3) animals.Add("mouse");
if (condition4) animals.Add("crab");
string result = "Animal Type: " + string.Join(", ", animals);
Sign up to request clarification or add additional context in comments.

1 Comment

this really helped thanks! what if I know that the output can be only 1 of the names in the list, how should I adjust my result field? thanks
0

Firstly, to join the strings, I would look at using

String.Join Method

Concatenates the members of a constructed IEnumerable(Of T) collection of type String, using the specified separator between each member.

So you can try something like

List<string> vals = new List<string>();
if (condition1) vals.Add("dog");
if (condition2) vals.Add("cat");
if (condition3) vals.Add("mouse");
if (condition4) vals.Add("crab");

And then try something like

Format.String("Animal Type: {0}, String.Join(",", vals));

1 Comment

this really helped thanks! what if I know that the output can be only 1 of the names in the list, how should I adjust my result field? thanks
0

The closest direct match would be something along the lines of:

console.WriteLine(string.Format("Animal Type: {0}, {1}, {2}, {3}", (condition1 ? "dog", ""), (condition2 ? "cat", ""), (condition3 ? "mouse", ""), (condition4 ? "crab", ""))); // print to output

With your code you're declaring the variable only for the scope of the if.

Another approach would be to push them into a list, for example, something like:

var selected = new List<string>();
if (condition1 == true) selected.Add("dog");
if (condition2 == true) selected.Add("cat");
if (condition3 == true) selected.Add("mouse");
if (condition4 == true) selected.Add("crab");

console.WriteLine(string.Format("Animal Type: {0}", string.Join(", ", selected.ToArray()))); // print to output

Comments

0

i would do it with a HashSet like this

 var animals = new HashSet<string>();
 if (condition1) animals.Add("dog");
 if (condition2) animals.Add("cat");
 if (condition3) animals.Add("mouse");
 if (condition4) animals.Add("crab");
 string result = "Animal Type: " + string.Join(", ", animals); 

4 Comments

this really helped thanks! what if I know that the output can be only 1 of the names in the list, how should I adjust my result field? thanks
example of your input and output you expect?
var animals = new HashSet<string>(); if (condition1) animals.Add("dog"); if (condition2) animals.Add("cat"); if (condition3) animals.Add("mouse"); if (condition4) animals.Add("crab"); string result = "only 1 of the above conditions, e.i. mouse" assumption is that only 1 of the 4 conditions above can be true
even in that case the above answer is correcy. You will get a valid answer.
0
 string output = "Animal Type: ";
            if (condition1 == true) output  += "dog ,";
            if (condition2 == true) output += "cat ,";
            if (condition3 == true) output += "mouse ,";
            if (condition4 == true) output += "crab ,";

            output = output.Substring(0, output.Length - 2);

Comments

0

I would say, defining a list of strings and putting in your values if true would be the solution. After that, Join it with semicolon as delimiter.

List<string> outList = new List<string>();
if (true) outList.Add("dog");
if (false) outList.Add("cat");
if (true) outList.Add("mouse");
if (false) outList.Add("crab");
Console.Write(String.Format("Animal Type: {0}", String.Join(",", outList)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.