- Home /
Output a string list
I have a list that contains a total of 3 strings. I want to output the list. How do I do that?
I tried myList.ToString() but that prints out debug information and all I want to print are the three items from the list.
Can anyone help me figure this out? Thank you!
Answer by Spinnernicholas · Jan 02, 2014 at 06:51 PM
If it's like this:
List<string> list;
then:
foreach(string str in list)
{
//print str
}
@Spinnernicholas $$anonymous$$akes sense, but my string list is in another class so when I try to write the foreach it tells me that my list is a field and is treated like a type
add a toString method to the class with the list and put the foreach loop in it. then call toString from the class you need it.
public string ToString()
{
string temp = "";
foreach(string str in list)
{
temp += str; //maybe also + '\n' to put them on their own line.
}
return temp;
}
@Spinnernicholas the good news is that it is storing the contents of the list, however, when I call it in the desired class nothing shows.
ToString will just return the strings, you will have to then display it somehow. To test if it is working, send it to debug.
Log.Debug(yourClass.ToString());
Your answer
![](https://koobas.hobune.stream/wayback/20220613125358im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Making a camera list 1 Answer
C# Randomly Adding Elements from stringListA to stringListB 1 Answer
List Bug when using debugger 1 Answer
C# GameObject Lists 2 Answers